%% Convert from DAT Rate to CD Sample Rate
% Change the sample rate of a signal by a rational conversion factor from the DAT rate of 48 kHz to the CD sample rate of 44.1 kHz. Use the rat function to find the numerator L and the denominator M of the rational factor.

Fdat = 48e3;
Fcd = 44.1e3;
[L,M] = rat(Fcd/Fdat)

% Generate a 1.5 kHz sinusoid sampled at f_{\textrm{DAT}} for 0.25 seconds. Plot the first millisecond of the signal.

t = 0:1/Fdat:0.25-1/Fdat;
x = sin(2*pi*1.5e3*t);
stem(t,x)
xlim([0 0.001])
hold on

% Design an antialiasing lowpass filter using a Kaiser window. Set the filter band edges as 90% and 110% of the cutoff frequency, \left(f_{\textrm{DAT}} /2\right)\times \min \left(1/L,1/M\right). Specify a passband ripple of 5 dB and a stopband attenuation of 40 dB. Set the passband gain to L.

f = (Fdat/2)*min(1/L,1/M);
d = designfilt('lowpassfir', ...
    'PassbandFrequency',0.9*f,'StopbandFrequency',1.1*f, ...
    'PassbandRipple',5,'StopbandAttenuation',40, ...
    'DesignMethod','kaiserwin','SampleRate',48e3);
h = L*tf(d);

% Use upfirdn with the filter h to resample the sinusoid. Compute and compensate for the delay introduced by the filter. Generate the corresponding resampled time vector.

y = upfirdn(x,h,L,M);

delay = floor(((filtord(d)-1)/2-(L-1))/L);
y = y(delay+1:end);
t_res = (0:(length(y)-1))/Fcd;

%
% Overlay the resampled signal on the plot.

stem(t_res,y,'*')
legend('Original','Resampled','Location','southeast')
hold off

% Copyright 2015 The MathWorks, Inc.