Reputation: 23
I think i'm implementing the code wrong. I thoughts that the original and zero padded signal would be concatenated around the same point, with the same peaks. Is my understanding of this wrong or is my code the issue?
clc;clear;
N=257; %number of points in the signal
f=330.5; %frequency of signal
fs=1024; %sampling frequency
Ts=1/fs; %sampling period
ts=0:Ts:(N-1)/fs; %duration of signal
x=sin(f*ts);%generation of sampled signal
X=fftshift(fft(x)); %shifted FFT of signal
figure(5)
stem(abs(X))
M=2048; %number of points desired in the new signal that will be zero padded
zerovec=zeros(1,(M-N)); %creating enough 0's to add to the end of the original signal to achieve the desired length
x1=[x zerovec]; %concatenating original signal and 0's to get zero padded signal
X1=fftshift(fft(x1)); %fft of zero padded signal
figure()
stem(abs(X)) %discrete plot of original signal
hold on
stem(abs(X1)) %discrete plot of zero padded signal
Upvotes: 2
Views: 433
Reputation: 60444
When zero-padding the signal, its frequency spectrum becomes more dense. In a sense, you interpolate in the frequency domain when zero-padding the spatial domain.
If you plot the two frequency spectra with the correct frequencies along the x-axis you'll see them overlapping:
N=257;
f=330.5;
fs=1024;
Ts=1/fs;
ts=0:Ts:(N-1)/fs;
x=sin(f*ts);
X=fftshift(fft(x));
F=0:fs/N:fs-fs/N; % <<< NEW!
M=2048;
zerovec=zeros(1,(M-N));
x1=[x zerovec];
X1=fftshift(fft(x1));
F1=0:fs/M:fs-fs/M; % <<< NEW!
figure()
stem(F,abs(X)) % <<< NEW! using F
hold on
stem(F1,abs(X1)) % <<< NEW! using F1
Upvotes: 1