Magnitude Plot Incorrect Frequency

조회 수: 4 (최근 30일)
Matt
Matt 2025년 3월 30일
댓글: Matt 2025년 3월 30일
Hi there, I'm having a problem plotting the magnitude spectrum of simple cos wave.
dt = .001;
t = -.5:dt:.5;
x1 = 2*cos(2*pi*20*t);
Wmax = 2*pi*100;
K = 2000;
w = (0:K-1) * Wmax / K;
X1 = x1 * exp(-j * t' * w) * dt;
mag_X1 = 2 * abs(X1);
display (X1);
subplot(2,1,1);
plot(t, x1);
xlabel('time');
ylabel('x(t)');
title('time domain');
subplot(2,1,2);
plot(w/2*pi, mag_X1);
axis([0 1000 0 7]);
xlabel('frequency Hz');
ylabel('|x(w)|');
title('magnitude spectrum');
The problem I'm having is that the plot shows a spike at 200 Hz rather than 20 Hz, if somebody could help me understand where I'm going wrong it would be greatly appreciated.

채택된 답변

Paul
Paul 2025년 3월 30일
Recheck the conversion from rad/sec to Hz in the plot command for the magnitude of the spectrum
  댓글 수: 3
Paul
Paul 2025년 3월 30일
Yes, the relationship is. Is that what the code implements?
Suppose omega = 6.28 so that f is approximately equal to 1. The code in the question would compute f as
w = 6.28;
f = w/2*pi
f = 9.8646
which is incorrect. Recall that Matlab follows PEMDAS for the basic math operators (Operator Precedence), so that computation for f is effectively being evaluated as
f = (w/2)*pi
f = 9.8646
Matt
Matt 2025년 3월 30일
Thanks so much, can't believe how silly I am.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Hassaan
Hassaan 2025년 3월 30일
편집: Hassaan 2025년 3월 30일
% Time step
dt = 0.001;
% Time vector
t = -0.5 : dt : 0.5;
% Define the signal: x1 = 2 cos(2π·20t)
x1 = 2 * cos(2*pi*20*t);
% Maximum angular frequency (rad/s)
Wmax = 2*pi*100;
% Number of frequency samples
K = 2000;
% Frequency vector in rad/s
w = (0 : K-1) * (Wmax / K);
% Compute Fourier transform via integration (numerical)
X1 = x1 * exp(-1j * t' * w) * dt;
% Magnitude (scaled by 2 for plotting)
mag_X1 = 2 * abs(X1);
% --- Plot time-domain signal ---
figure;
subplot(2,1,1);
plot(t, x1);
xlabel('Time (s)');
ylabel('x(t)');
title('Time Domain');
% --- Plot magnitude spectrum ---
subplot(2,1,2);
% Use w/(2*pi) to convert rad/s to Hz
plot(w/(2*pi), mag_X1);
xlabel('Frequency (Hz)');
ylabel('|X(\omega)|');
title('Magnitude Spectrum');
% Optional axis limits for clarity
axis([0 100 0 7]);
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

카테고리

Help CenterFile Exchange에서 Spectral Measurements에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by