Skip to content

Instantly share code, notes, and snippets.

@tobin
Created October 18, 2012 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobin/3910858 to your computer and use it in GitHub Desktop.
Save tobin/3910858 to your computer and use it in GitHub Desktop.
A look at the frequency response of sample-and-hold (aka boxcar filter)
%% Frequency response of a boxcar filter
%
% A look at the frequency response of sample-and-hold.
%
% Tobin Fricke 2012-10-18
Fs = 2^16; % Higher sample rate
R = 32; % upsampling factor
N = 1 * Fs; % number of sample data to generate
tau = (1/Fs) * R; % lower sampling period
my_boxcar = ones(R, 1)/R;
% Compute the transfer function by applying the filter to random data
x = randn(N, 1);
y = filter(my_boxcar, 1, x);
nfft = N/16;
[Txy, f] = tfestimate(x, y, hanning(nfft), nfft/2, nfft, Fs);
% Analytic form, for comparison
H = sinc(R*f/Fs) .* exp(-2i*pi*f*tau/2);
subplot(2,1,1)
semilogx(f, db(Txy), '.',...
f, db(H), '-');
ylim([-60 10]);
legend('numerical (tfestimate)', 'analytic (sinc+delay)')
title(sprintf('Frequency response of %dX sample-and-hold', R));
subplot(2,1,2);
semilogx(f, unwrap(angle(Txy))*180/pi, ...
f, unwrap(angle(H) * 180/pi));
%xlim([0 (Fs/R/2)]); % look at only the in-band region
set(gca,'ytick', (-10:1:1) * 360);
orient landscape
print -dpdf boxcar_test.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment