%% Upconvert and Downconvert a Sine Wave Signal
% Create a digital up converter object that up samples a 1 KHz sinusoidal signal by a factor of 20 and up converts it to 50 KHz. Create a digital down converter object that down converts the signal to 0 Hz and down samples it by a factor of 20.
% Note: If you are using R2016a or an earlier release, replace each call to the object with the equivalent step syntax. For example, obj(x) becomes  step(obj,x).
% Create a sine wave generator to obtain the 1 KHz sinusoidal signal with a sample rate of 6 KHz.

Fs = 6e3; % Sample rate
sine = dsp.SineWave('Frequency',1000,'SampleRate',... 
Fs,'SamplesPerFrame',1024);
x = sine(); % generate signal

%
% Create a DigitalUpConverter object. Use minimum order filter designs and set passband ripple to 0.2 dB and the stopband attenuation to 55 dB. Set the double sided signal bandwidth to 2 KHz.

upConv = dsp.DigitalUpConverter(... 
 'InterpolationFactor', 20,...
 'SampleRate', Fs,...
 'Bandwidth', 2e3,...
 'StopbandAttenuation', 55,...
 'PassbandRipple',0.2,...
 'CenterFrequency',50e3);

%
% Create a DigitalDownConverter object. Use minimum order filter designs and set the passband ripple to 0.2 dB and the stopband attenuation to 55 dB.

dwnConv = dsp.DigitalDownConverter(...
  'DecimationFactor',20,...
  'SampleRate', Fs*20,...
  'Bandwidth', 3e3,...
  'StopbandAttenuation', 55,...
  'PassbandRipple',0.2,...
  'CenterFrequency',50e3);

%
% Create a spectrum estimator to visualize the signal spectrum before up converting, after up converting, and after down converting.

window = hamming(floor(length(x)/10));
figure; pwelch(x,window,[],[],Fs,'centered')
title('Spectrum of baseband signal x')

%
% Up convert the signal and visualize the spectrum

xUp = upConv(x); % up convert
window = hamming(floor(length(xUp)/10));
figure; pwelch(xUp,window,[],[],20*Fs,'centered');
title('Spectrum of up converted signal xUp')

%
% Down convert the signal and visualize the spectrum

xDown = dwnConv(xUp); % down convert
window = hamming(floor(length(xDown)/10));
figure; pwelch(xDown,window,[],[],Fs,'centered')
title('Spectrum of down converted signal xDown')

%
% Visualize the response of the decimation filters

visualizeFilterStages(dwnConv)

% Copyright 2012 The MathWorks, Inc.