Add zero padding to fft

207 views (last 30 days)
S
S on 1 Apr 2024
Commented: Star Strider on 5 Apr 2024
I am trying to add zero padding to my fft but am unsure where I would add it, I have tried putting it at the end of the fft_spectrum line but that isn't working. Thank you for your time!
%% perform FFT of signal :
[freq,fft_spectrum] = do_fft(t,output_sum);
figure
plot(freq,fft_spectrum,'-*')
xlim([0 1000]);
title('FFT of output sum signal')
ylabel('Amplitude');
xlabel('Frequency [Hz]')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [freq_vector,fft_spectrum] = do_fft(time,data)
time = time(:);
data = data(:);
dt = mean(diff(time));
Fs = 1/dt;
nfft = length(data); % maximise freq resolution => nfft equals signal length
%% use windowing or not at your conveniance
% no window , zero padd!!!
fft_spectrum = abs(fft(data))*2/nfft;
% % hanning window
window = hanning(nfft);
window = window(:);
fft_spectrum = abs(fft(data.*window))*4/nfft;
% one sidded fft spectrum % Select first half
if rem(nfft,2) % nfft odd
select = (1:(nfft+1)/2)';
else
select = (1:nfft/2+1)';
end
fft_spectrum = fft_spectrum(select,:);
freq_vector = (select - 1)*Fs/nfft;
end

Accepted Answer

Star Strider
Star Strider on 1 Apr 2024
Edited: Star Strider on 2 Apr 2024
Let the fft function add it to the original time domain signal vector (it pads it at the end) by specifying the second argument to be greater than the signal length.
Using:
NFFT = 2^nextpow2(L);
where ‘L’ is the signal length is best, because lengths of powers-of-2 (not limited to ‘L’, and can be several times that length so long as it is a power-of-2 and fits in memory) really is more efficient and faster (I actually timed it a while back). This also increases the frequency resolution, always a good thing (in my opinion).
As for calculating the one-sided Fourier transformm, I coded ‘FFT1’ a while back for my own use, and have posted it it here in some of my answers —
Fs = 44100;
L = 1;
t = linspace(0, Fs*L, Fs*L+1).'/Fs; % Time Vector (Assume Column Vectors)
s = sin(2*pi*t*(1:1000:2.2E4)); % Signal Vector Matrix
[FTs,Fv] = FFT1(s,t); % Call 'FFT1' Function
figure
plot(Fv, abs(FTs)*2) % Plot Results
grid
xlim([0 max(Fv)])
axis('padded')
xlabel('Frequency (Hz)')
ylabel('Magnitude')
function [FTs1,Fv] = FFT1(s,t)
t = t(:);
L = numel(t);
if size(s,2) == L
s = s.';
end
Fs = 1/mean(diff(t));
Fn = Fs/2;
NFFT = 2^nextpow2(L);
FTs = fft((s - mean(s)) .* hann(L).*ones(1,size(s,2)), NFFT)/sum(hann(L));
Fv = linspace(0, 1, NFFT/2+1)*Fn;
Iv = 1:numel(Fv);
FTs1 = FTs(Iv,:);
end
The online Run feature is still not working (it didn’t work all day yesterday either), so I can’t demonstrate it here, however I again verified that it works (in MATLAB Online).
EDIT — (2 Apr 2024 at 10:37)
The Run feature is back, so I ran the code.
.
  4 Comments
S
S on 5 Apr 2024
@Star Strider oh ok! wait so I'm trying to understand windowing vs zero padding.
Zero padding is adding zeros to the beginning and end to better resolution and windowing calculates data values within a certain window to improve accuracy? Do I have that right?
Star Strider
Star Strider on 5 Apr 2024
In MATLAB, zero-padding is usually done at the end of the vector. (Zero-padding to an integer power-of-2 increases the fft efficiency because the fft algorithm works best in that instance.) It has the definite additional advantage of increasing the frequency resolution.
Windowing (for example with a hann window) corrects for the finite nature of a discrete Fourier transform, so the result more closely matches the result of an analytic Fourier transform (that by definition integrates from .to ).

Sign in to comment.

More Answers (0)

Categories

Find more on Fourier Analysis and Filtering 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!