Sampling and different methods of reconstruction --- a MATLAB experiment for Purdue ECE 301, by Prof. Chih-Chun Wang.
Preparation (different from the previous MATLAB experiments)
1. Download the different wav files x1.wav (Save it as “x1.wav”.)
2. Download the following m files: ece301conv.m, ece301_sampler.m, ece301_ZOH.m, ece301_Lin.m, ece301_Opt.m.
3. Open MATLAB and change the directory to where you have saved the previous files.
4. Enter the following commands
duration=8;
f_sample=44100;
t=(((0-4)*f_sample+0.5):((duration-4)*f_sample-0.5))/f_sample;
Basic operations:
1. If you would like to sample a continuous-time signal x with sampling period 0.2 sec, then use the following command.
x=sin(t);
T=0.2;
[xn, sampling_index, sampling_locations]=ece301_sampler(x,T);
Remark: A question you should as yourself: Can you predict the size (dimension) of the array xn? (Hint: To answer this question, you need to know that the original continuous time signal x is for a range 8 seconds from -4<t<4. The range does not include the end points -4 and 4.)
You can use the following command
length(xn)
to verify whether your prediction is correct.
If you would like to plot the new discrete time signal x[n], use the following command
stem(sampling_index,
xn);
xlabel('n');
ylabel('x[n]');
If you would like to place the sampled values in their corresponding positions, use the following command
plot(sampling_locations,
xn, 'o');
xlabel('t (sec)');
ylabel('x(nT)');
Remark: Notice the change of the time-axis.
You can also compare the continuous-time signal and the sampled values by plotting x and x(nT) together in a single figure.
plot(t,
x, sampling_locations, xn, 'ro');
xlabel('t (sec)');
legend('x(t)', 'x(nT)');
Requirement: For this homework assignment, you are asked to plot several figures. Print all the figures for each objective. Your HW score will be graded based on your figures. There is no need to turn in your .m files.
Objective 1: Synthesize the US dial tone. The US dial tone consists of two frequencies: 440Hz and 350 Hz. Let us use MATLAB to synthesize the US dial tone.
s1=sin(2*pi*440*t);
s2=sin(2*pi*350*t);
xt=s1+s2;
sound(xt,f_sample);
Sample the dialtone signal with a sampling frequency 4000Hz by the following command. (This command takes some time to finish. The actual running time depends on your computer. Your T value depends on your sampling frequency.)
[xn, sampling_index, sampling_locations]=ece301_sampler(xt,T);
Compare the sampled values with the continuous time signal by the following commands:
plot(t,xt, sampling_locations, xn, 'ro');
xlabel('t (sec)');
axis([0.2, 0.22, -2,2])
legend('conti. signal', 'sampled values');
Use the following commands to compare the size of the new sampled array and the original continuous time signal:
whos
xt;
whos xn;
[Discussion] Have you noticed that the size of the array is reduced since now we only have roughly 8sec * 4000 (samples per second) = 32000 samples?
Objective 2: Download w_samples.mat file. Use the following command to load a different sampled array w[n] with sampling period 0.7.
load w_samples;
Take a look at the sampled values by the following commands:
stem(sampling_index,
wn);
xlabel('n');
ylabel('w[n]');
Place the sampled values in the right location by the following commands:
plot(sampling_locations,
wn, 'ro');
xlabel('t (sec)');
ylabel('w(nT)');
Perform Zero-Order Hold to reconstruct the original signal by the following command:
wtZ=ece301_ZOH(wn, T);
Perform Linear interpolation to reconstruct the original signal by the following command:
wtL=ece301_Lin(wn, T);
Perform optimal (bandlimited) reconstruction by the following command:
wtO=ece301_Opt(wn, T);
[Discussion] Would you be able to predict the shapes of the signals wtZ, wtL, wtO?
Run the following commands to verify your predictions.
plot(sampling_locations, wn, 'ro', t, wtZ, t, wtL, t, wtO );
xlabel('t (sec)');
legend('sampled_values', 'ZOH', 'Linear', 'Optimal');
Objective 3: Zero-Order Hold: Reconstruct the original dial tone signal x(t) by the following command: (You need to change the sampling period T back to the correct value as was used in Objective 1 since we are no longer dealing with the w signals.) (These commands take some time to finish. The actual running time depends on your computer.)
[xn, sampling_index, sampling_locations]=ece301_sampler(xt,T);
Zero-Order Hold: Reconstruct the original signal by the following command:
ytZ=ece301_ZOH(xn, T);
Linear interpolation: Reconstruct the original signal by the following command:
ytL=ece301_Lin(xn, T);
Optimal (bandlimited) reconstruction: Reconstruct the original signal by the following command:
ytO=ece301_Opt(xn, T);
Compare the above three different methods of reconstruction by the following commands.
plot(t,xt, sampling_locations, xn, 'ro', t, ytZ, t, ytL, t, ytO
);
xlabel('t (sec)');
axis([0.2035, 0.207, -0.7,0.2])
legend('conti. signal', 'sampled_values', 'ZOH', 'Linear', 'Optimal');
Use the following commands to listen to the reconstructed signals.
sound(ytZ,
f_sample);
sound(ytL, f_sample);
sound(ytO, f_sample);
[Discussion] Have you noticed that the optimal reconstruction closely fits the original continuous signal?
Objective 4: Redo Objectives 1 and 3, but this time use the sampling frequency 1000Hz. When repeating Objective 3, use the following commands to plot the figure.
plot(t,xt, sampling_locations, xn, 'ro', t, ytZ, t, ytL, t, ytO
);
xlabel('t (sec)');
axis([0.2, 0.22, -2,2])
legend('conti. signal', 'sampled_values', 'ZOH', 'Linear', 'Optimal');
[Discussion] Have you noticed that the size of the sampled values is even smaller?
[Discussion] Compare the (sound) quality degradation of ZOH, Linear, and Optimal reconstruction by figures and by listening to the signals.
Objective 5: Redo Objectives 1 and 3, but this time use the sampling frequency 500Hz. When repeating Objective 3, use the following commands to plot the figure.
plot(t,xt, sampling_locations, xn, 'ro', t, ytZ, t, ytL, t, ytO
);
xlabel('t (sec)');
axis([0.2, 0.22, -2,2])
legend('conti. signal', 'sampled_values', 'ZOH', 'Linear', 'Optimal');
[Discussion] Is Optimal reconstruction still able to reconstruct the original dial tone signal?
[Discussion] Can you predict what you will hear by quantifying the “aliasing effect”?
[Optional, no need to turn in any figure for this objective] Objective 6: Load the music signal x1 and convert it to its band-limited version by the following commands.
[x1, f_sample]=audioread('x1.wav');
x1=x1';
x1_LPF=ece301conv(x1, sin(2*pi*2000*t)./(pi*t));
Compare these two signals by the following commands.
plot(t,x1, t, x1_LPF);
xlabel('t (sec)');
axis([-2.2715, -2.2685, 0.025, 0.052]);
legend('conti. signal', 'band-limited ver.');
Since we are now focusing on a band-limited signal with bandwidth 2000Hz, the minimum sampling frequency is 2*2000Hz. Use a sampling frequency 5000Hz and perform the following sampling operation.
[xn, sampling_index,
sampling_locations]=ece301_sampler(x1_LPF,T);
ytO=ece301_Opt(xn, T);
Compare the reconstructed signal and the original band-limited version by the following commands:
plot(t, x1_LPF, t, ytO);
xlabel('t (sec)');
axis([-2.2715, -2.2685, 0.025, 0.052]);
legend('band-limited ver.', 'Opt. Reconstruction');
Listen to the signals by
sound(x1_LPF,
f_sample);
sound(ytO, f_sample);
[Discussion] Are x1_LPF and ytO (almost) identical?
[Discussion] You may like to lower the sampling frequency and see how the sampling frequency affects the sound quality.