MATLAB Answers

Fast Fourier Transform Zero Padding

72 views (last 30 days)
kyle mani
kyle mani on 23 Jan 2021
Commented: Walter Roberson on 24 Jan 2021
Hi all,
I am using the code shown below to plot the FFT of some data. My issue is that the "resolution" seems poor, as the x axis is in increments of 0.2. I would like much finer plotting of points, and have recently seen the Zero Padding method. However, everytime I try to implement other solutions on MATLAB answers, I cannot seem to increase the resolution. Could anyone help me with the necessary code for my specific case?
O2_exp = [0.0247
0.2372
1.9171
1.5570
0.8016
0.5572
1.2185
1.3601
1.0067
0.7767
1.0244
1.1619
1.0210
0.8791
0.9595
1.0592
1.0274
0.9507
0.9735
1.0303
1.0286
0.9912
0.9924
1.0137
1.0143
0.9982
0.9996
1.0097
1.0174
1.0062
1.0052
1.0115
1.0177
1.0131
1.0150
1.0117
1.0182
1.0153
1.0206
1.0177
1.0243
1.0200
1.0221
1.0207
1.0235
1.0256
1.0275
1.0237
1.0248
1.0264];
figure
Fs = 1;
L = length(O2_exp);
Y = fft(O2_exp);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
plot(f(2:end),P1(2:end)/max(P1(2:end)),'color','red','linewidth',4)

  2 Comments

Matt J
Matt J on 23 Jan 2021
I cannot see anywhere in your code where you have attempted zero-padding.
kyle mani
kyle mani on 23 Jan 2021
Hi Matt! I have attempted to use zero-padding, but I am getting errors. Essentially, I have doubled the O2 vector to length 100, and have kept the first 50 values to be zero. However, it completely distorts the graph that I am seeing without the zero-padding.
The code I pasted above works perfectly in generating the FFT I would like, except the resolution is very poor. Therefore, I am asking if anyone can use the zero-padding method or any other appropriate method with the code I have specifically provided to achieve my goal of increasing the resolution for this graph.

Sign in to comment.

Accepted Answer

Paul
Paul on 23 Jan 2021
Try changing these lines:
L = length(O2_exp);
Y = fft(O2_exp);
to
L = nfft; % select nfft > numel(O2_exp), preferable a power of 2
Y = fft(detrend(O2_exp),nfft)

  4 Comments

Show 1 older comment
Walter Roberson
Walter Roberson on 23 Jan 2021
Yes, but the output you get is no longer physically meaningful.
When you request an fft with more points than you have input for, then it proceeds by zero-padding the input to the number of points. In the frequency domain that is the same as convolution with the sync function.
The fundamental assumption for fft is that the input represents a number of whole cycles extracted out of infinitely repeated data. For example, if the input is
/\
/ \
then it is mathematically treated as if it were an extract from
/\ /\ /\ /\ /\ /\
/ \/ \/ \/ \/ \/ \
whereas when you use a larger number of points in the nfft field, you are treating it as if it were
/\ ____________________
/ \
where __ is the zero line.
Any additional sharpness you appear to observe in your plotting is an illusion.
If you need meaningful finer resolution, then you need more data, either recording the data for longer or else recording it for the same time but at higher sampling rate.
Paul
Paul on 24 Jan 2021
I'm not so sure I'd go so far as to say the output isn't physically meaningful, but I understand the sentiment.
I do agree that in order to get finer resolution, more data is needed.
However, I took the OP's question to not really mean frequency domain resolution in the technical sense (i.e, the ability the distinguish among frequency components); rather the OP was trying to get a reasonable interpolation in the frequency domain, of which zero padding is a reasonable approach among others. This link (and the Next pages) are a good discussion.
The results always need to be understood in the context of the underying samples of data.
Walter Roberson
Walter Roberson on 24 Jan 2021
According to the comment at https://www.mathworks.com/matlabcentral/answers/724683-fast-fourier-transform-zero-padding#comment_1280732 the poster wanted to reduce the uncertainty in the interpretation of the frequency values. However, that is not something that you can do using zero padding.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 23 Jan 2021
Pad_factor = 5;
O2_exp = [0.0247
0.2372
1.9171
1.5570
0.8016
0.5572
1.2185
1.3601
1.0067
0.7767
1.0244
1.1619
1.0210
0.8791
0.9595
1.0592
1.0274
0.9507
0.9735
1.0303
1.0286
0.9912
0.9924
1.0137
1.0143
0.9982
0.9996
1.0097
1.0174
1.0062
1.0052
1.0115
1.0177
1.0131
1.0150
1.0117
1.0182
1.0153
1.0206
1.0177
1.0243
1.0200
1.0221
1.0207
1.0235
1.0256
1.0275
1.0237
1.0248
1.0264];
plot(O2_exp); title('original');
nO2 = numel(O2_exp);
reconstructed = ifft(fft(O2_exp,2*nO2));
plot(reconstructed); title('reconstructed nfft')
%caution: the details that follow are only valid when the
%length of the signal is even, and the signal is purely real.
F = fft(O2_exp);
Fpad = [F(1); F(2:end/2+1); zeros(Pad_factor*nO2-1,1); flipud(conj(F(2:end/2+1)))];
reconstructed_center_padded = ifft(Fpad);
plot(reconstructed_center_padded); title('reconstructed center padded')

  3 Comments

Walter Roberson
Walter Roberson on 23 Jan 2021
Note that zero padding in the frequency domain is equivalent to convolution of the signal with a sync function, so it is no illusion that the reconstructed center padded version has more bumps: it really does have more bumps, introduced by the sync signal convolution.
Truly increasing the resolution involves introducing extra information. Otherwise you are at best smoothing the signal to make it look nice, which does the opposite, removes information.
kyle mani
kyle mani on 23 Jan 2021
Hi Walter, thank you for your answer. When I run the code I provided I get this image for the FFT, which is how it should look:
I guess my question is: Is there any way to increase the resolution of this image so that the x-axis is not just plotting points by increments of 0.02? The reason why is 1/frequency will give me the periodicity of these oxygen oscillations. In this graph, the peak is at a value of 0.24, corresponding to a periodicity of 4.16, and based on the plotting the increments directly greater and less would result in periods of: 4.54 and 0.386, which seemingly lead to large variation.
Walter Roberson
Walter Roberson on 23 Jan 2021
You can change how you process to get it to plot at most any increment. The problem is that your output stops becoming meaningful. If you need finer resolution then you need more data.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!