MATLAB Answers

Magnitude and phase spectrum of sine wave using FFT

5 views (last 30 days)
Cynthia Lucy
Cynthia Lucy on 7 May 2021
Commented: Cynthia Lucy on 10 May 2021
I want to display the magnitude and phase spectrum of a signal data using the code below.
load("sec2Harmonics.mat")
Length = numel(x) % The number of elements
fs = 44100;
amp = 1000; % Amplitude (coefficient)
Plength = Length;
plot(x(1:Plength)); % Plot graph
FFT_len = 1024; % 1024 points;
plot_FFT_len = FFT_len/2;
d_proc1 = x(1:FFT_len);
win = ?????
d_proc2 = ?????;
fftd = fft(d_proc2);
delta_f = fs/FFT_len;
n = 0:1:FFT_len-1;
w = delta_f*n;
plot(w(1:plot_FFT_len), 20*log10(abs(?????))(1:plot_FFT_len));
Ive tried to replace the '?????' as below but when I run the code it only display the sine wave. Theres no error on the command window I dont know what is wrong and what should be fixed. Please help me.
Length = numel(x) % The number of elements
fs = 44100;
amp = 1000; % Amplitude (coefficient)
Plength = Length;
subplot(2,1,1);
plot(x(1:Plength)); % Plot graph
FFT_len = 512; % 512 points;
plot_FFT_len = FFT_len/2;
d_proc1 = x(1:FFT_len);
win = 0.5 - 0.5*cos(2*pi*linspace(0, 1, n));
win = (Plength);
d_proc2 = x.*win;
fftd = fft(d_proc2);
delta_f = fs/FFT_len;
n = 0:1:FFT_len-1;
w = delta_f*n;
subplot(2,1,2);
plot(w(1:plot_FFT_len), 20*log10(abs(fft/Plength))(1:plot_FFT_len));
This is the display result that I got.

Answers (1)

VBBV
VBBV on 7 May 2021
%f tru
subplot(2,1,2);
plot(w(1:plot_FFT_len), 20*log10(abs(fftd./Plength)).*(1:plot_FFT_len));
  10 Comments
Cynthia Lucy
Cynthia Lucy on 10 May 2021
@VBBV yes an error came out on command window after I run the latest code saying 'error: product: nonconformant arguments (op1 is 1x1102, op2 is 1x512)'

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!