# Applying zero padding and windowing in my code

18 views (last 30 days)
Jmv on 13 Apr 2020
Commented: Peng Li on 2 Jun 2020
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?
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 on 14 Apr 2020
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 CommentsShowHide 1 older comment
Peng Li on 2 Jun 2020
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?

### Categories

Find more on Spectral Measurements 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!

Translated by