fft of a signal
조회 수: 14 (최근 30일)
이전 댓글 표시
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
채택된 답변
Star Strider
2022년 9월 27일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Filter Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!