%% Resample Using Kaiser Window
% Construct a sinusoidal signal. Specify a sample rate such that 16 samples correspond to exactly one signal period. Draw a stem plot of the signal. Overlay a stairstep graph for sample-and-hold visualization.

fs = 16;
t = 0:1/fs:1-1/fs;

x = 0.75*sin(2*pi*t);

stem(t,x)
hold on
stairs(t,x)
hold off

%
% Use resample to upsample the signal by a factor of four. Use the default settings. Plot the result alongside the original signal.

ups = 4;
dns = 1;

fu = fs*ups;
tu = 0:1/fu:1-1/fu;

y = resample(x,ups,dns);

stem(tu,y)
hold on
stairs(t,x)
hold off
legend('Resampled','Original')

%
% Repeat the calculation. Specify n = 1 so that the antialiasing filter is of order 2\times1\times4=8. Specify a shape parameter \beta=0 for the Kaiser window. Output the filter as well as the resampled signal.

n = 1;
beta = 0;

[y,b] = resample(x,ups,dns,n,beta);

fo = filtord(b)

stem(tu,y)
hold on
stairs(t,x,'--')
hold off
legend('n = 1, \beta = 0')

%
% The resampled signal shows aliasing effects that result from the relatively wide mainlobe and low sidelobe attenuation of the window.
% Increase n to 5 and leave \beta=0. Verify that the filter is of order 40. Plot the resampled signal.

n = 5;

[y,b] = resample(x,ups,dns,n,beta);

fo = filtord(b)

stem(tu,y)
hold on
stairs(t,x,'--')
hold off
legend('n = 5, \beta = 0')

%
% The longer window has a narrower mainlobe and attenuates aliasing effects better. It also attenuates the signal.
% Leave the filter order at 2\times5\times4=40 and increase the shape parameter to \beta=20.

beta = 20;

y = resample(x,ups,dns,n,beta);

stem(tu,y)
hold on
stairs(t,x,'--')
hold off
legend('n = 5, \beta = 20')

%
% The high sidelobe attenuation results in good resampling.
% Decrease the filter order back to 2\times1\times4=8 and leave \beta=20.

n = 1;

[y,b] = resample(x,ups,dns,n,beta);

stem(tu,y)
hold on
stairs(t,x,'--')
hold off
legend('n = 1, \beta = 20')

% The wider mainlobe generates considerable artifacts upon resampling.
% Copyright 2012 The MathWorks, Inc.