Applying zero padding and windowing in my code

56 次查看(过去 30 天)
Hi,
I have the following working code perfoming FFT analysis. I am still learning mathlab and signal processing however, I have been reading that in order to get a better FFT output one must apply zero padding and windowing.
I have come up with the following code with the help of online tutorials and this forum. The code is giving me what I want which is frequency components in the signal however i don't think it is achieving this doing zero padding and windowing.
My question when is it necessary to perform zero padding and windowing on the signal? and how can incorporate it in my code?
[D,S,R] = xlsread('test data.csv');
t = D(:,1);
v = D(:,2);
figure
plot(t, v); % plot time domain signal
grid
xlabel('Time')
ylabel('Voltage')
Signal = D(:,2);
Ts = 0.00005; % Sampling Interval (seconds)
Fs = 1/Ts; % Sampling Frequency (Hz)
Fn = Fs/2; % Nyquist Frequency (Hz) half of the sampling rate of a discrete signal processing system
N = length(Signal);
meanSignal = mean(Signal); % ‘Signal’ Mean
FTSignal = fft(Signal-meanSignal)/N; % Normalised Fourier Transform Of Baseline-Corrected ‘Signal’
Fv = linspace(0, 1, fix(numel(FTSignal)/2)+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
[pks,locs] = findpeaks(abs(FTSignal(Iv))*2, 'MinPeakHeight',0.044);
figure
plot(Fv, abs(FTSignal(Iv))*2)
grid
xlabel('Frequency(Hz)')
ylabel('Amplitude')
plotIdx = 1:Iv(max(locs));
figure
plot(Fv(plotIdx), abs(FTSignal(Iv(plotIdx)))*2)
hold on
plot(Fv(plotIdx(locs)), pks, '^r', 'MarkerFaceColor','r')
title('FFT for Power Analysis')
xlabel('Frequency (Hz)')
ylabel('Amplitude')
xlswrite('myfile.xls',Fv')
xlswrite('myfile2.xls',abs(FTSignal(Iv))*2')
hold off

采纳的回答

Peng Li
Peng Li 2020-4-14
Based on your code, no zero padding is done and no window function is applied. To do this, you can give fft a second input for fft length. If it is larger than the actual length of the signal, zero padding will be done automatically. for example:
FTSignal = fft(Signal-meanSignal, 10240)/N;
You can apply a window function by this
FTSignal = fft((Signal(:)-meanSignal) .* hann(length(Signal)) ) / N;
By this, you will apply a hanning window.
There isn't anything that you must do honestly. But in general it is oftentimes true that by applying a window function before fft, you will better handle spectral leakage.
Applying zero padding helps with frequency resolution--> to have the spectral trace at the expected frequency. Your frequency resolution is equal to Fs/fftLength. But this doesn't help with differentiating two close frequency trace. To get better differetiation capability, you need longer signal.
  2 个评论
Jmv
Jmv 2020-5-29
Hi Peng Li. I was wondering if you can help with a followup question.
The code you suggested above for including a window returns amplitude (db) I want to display magnitude in lenear as I was doing without the window function. How can i include this window and stll get the linear magnitude.
Thanks
Peng Li
Peng Li 2020-6-2
fft results in absolute amplitude, not dB unless you transform what fft results using 20*log10(xxxx). So I'm not sure if I get the question you ask?

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by