fft of a signal
19 views (last 30 days)
Show older comments
Mangesh KAle
on 27 Sep 2022
Answered: Star Strider
on 27 Sep 2022
I would like to know whether the code is right or wrong because I am not getting the frequency plot
clc;
%% load a signal
[y,Fs] = audioread("bodytune.wav"); % Fs= Sampling Frequency
% y = 393216*2 double and Fs= 16000
info = audioinfo("bodytune.wav");
y1= y(:,1);
L = length(y); % Length of a signal
dt= 1/Fs;
t = (0:L-1)*dt; % Time vector
%% plot and label the graph
figure(1)
plot(t,y1);
xlabel('time')
ylabel('Amplitude')
grid on
%% fourier transform
nfft = 2^( nextpow2(length(y1)) );
df = Fs/nfft;
f = 0:df:Fs/2;
X_fft = fft(y1,nfft);
X_fft = X_fft(1:nfft/2+1);
figure(2);
plot(f,abs(X_fft));
xlabel('Frequency')
ylabel('Amplitude')
grid on
0 Comments
Accepted Answer
Star Strider
on 27 Sep 2022
You need to normalise the fft result by the length of the signal:
X_fft = fft(y1,nfft)/L;
Otherwise, using my test signal, it appears to be correct —
% clc;
% % load a signal
% [y,Fs] = audioread("bodytune.wav"); % Fs= Sampling Frequency
% y = 393216*2 double and Fs= 16000
% info = audioinfo("bodytune.wav");
Fs = 44100;
t = linspace(0, 5*Fs-1, 5*Fs)/Fs;
y = sum(sin([1; 5000; 10000; 15000]*2*pi*t)).';
y1= y(:,1);
L = length(y); % Length of a signal
dt= 1/Fs;
t = (0:L-1)*dt; % Time vector
%% plot and label the graph
figure(1)
plot(t,y1);
xlabel('time')
ylabel('Amplitude')
grid on
%% fourier transform
nfft = 2^( nextpow2(length(y1)) );
df = Fs/nfft;
f = 0:df:Fs/2;
X_fft = fft(y1,nfft)/L;
X_fft = X_fft(1:nfft/2+1);
figure(2);
plot(f,abs(X_fft));
xlabel('Frequency')
ylabel('Amplitude')
grid on
.
0 Comments
More Answers (0)
See Also
Categories
Find more on Filter Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!