Skip to content

Instantly share code, notes, and snippets.

@stephenHartzell
Created July 26, 2017 03:31
Show Gist options
  • Save stephenHartzell/170de1745e5ec1506e7618bb6b14efa9 to your computer and use it in GitHub Desktop.
Save stephenHartzell/170de1745e5ec1506e7618bb6b14efa9 to your computer and use it in GitHub Desktop.
Fs = 10; % Sampling frequency
Ts = 1/Fs; % Sampling period
N = 32; % Total number of samples
n = 0:N-1; % Samples
t = Ts*(n); % Sampled times
y = cos(2*pi*t) + .2*cos(2*pi*1.5*t); % Discrete signal
N = length(y); % The new total number of samples
Y = fft(y); % The fft of the signal
df = 1/(N*Ts); % The frequency resolution
k = 0:N-1; % Frequency spectrum samples
f = k*df; % Frequency values at the k samples
% Now lets make y length 128 by padding y with zeros
% p = "prime"
yp = y;
yp(length(y)+1:length(y)+96) = zeros(1,96);
Np = length(yp); % The new total number of samples
Yp = fft(yp); % The fft of the signal
dfp = 1/(Np*Ts); % The frequency resolution
kp = 0:Np-1; % Frequency spectrum samples
fp = kp*dfp; % Frequency values at the k samples
% Plot the fft of the signal
figure
subplot(2,1,1), plot(fp,abs(Yp))
title('Spectrum of Zero-Padded Signal')
ylabel('|fft(y)|')
xlabel('Frequency (Hz)')
subplot(2,1,2), plot(f,abs(Y))
title('Spectrum of non Zero-Padded Signal')
ylabel('|fft(y)|')
xlabel('Frequency (Hz)')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment