sampling continuos signals
TRANSCRIPT
-
8/11/2019 Sampling Continuos Signals
1/30
Laboratrio SDR
Analog to Digital Conversion Lab
UNICAMP IE309
1.Sampling and Aliasing
In signal processing,samplingis the reduction of a continuous signalto
a discrete signal. A common example is the conversion of a sound wave(a
continuous signal) to a sequence of samples (a discrete-time signal).
A sample refers to a value or set of values at a point in time and/or space.
A sampleris a subsystem or operation that extracts samples from a continuous
signal. A theoretical ideal samplerproduces samples equivalent to the
instantaneous value of the continuous signal at the desired points. In the Figure
1.1. we can see the presentation of a analog signal that has been sampled in
discrete time.
Figure 1.1.Signal sampling representation. The continuous signal is represented
with a green colored line while the discrete samples are indicated by the blue
vertical lines.
In this introduction we are going to see examples in sampling through Matlab
and Simulink, and finally we review a project to explore the aliasing in audio
tones signals.
-
8/11/2019 Sampling Continuos Signals
2/30
Experiment 1.1. Introduction in sampling in Matlab.
In this example we explore create the simulation of an analog signal (cosine
signal), and then create the sampled version, first a little over the Nyquist
Frequency and finally using oversampling to see the differences.
The example consists in
Create a cosine signal in a frequency of operation F0 = 60 Hz;
Sampling the original signal in 150 Hz, and the in 1 kHz.
Plot the analog signal, and the sampled versions.
Solution: The code that makes the experiment is show below, and the result is
showed in the Figure 1.2.
t = 0:1/2000:.02;x = cos(2*pi*60*t); % approx. to continuous-timet1 = 0:1/150:.02;n1 = 0:length(t1)-1;x150 = cos(2*pi*60/150*n1); % fs = 150 Hzt2 = 0:1/1000:.02;n2 = 0:length(t2)-1;x1000 = cos(2*pi*60/1000*n2); % fs = 1000 Hz
subplot(3,1,1)
plot(t,x)title('Analog signal')xlabel('time')ylabel('Amplitude')
subplot(3,1,2)stem(n1,x150,'filled')title('Sampled at 150 Hz')xlabel('n')ylabel('Amplitude')
subplot(3,1,3)
stem(n2,x1000,'filled')title('Samples at 1kHz')xlabel('n')ylabel('Amplitude')
-
8/11/2019 Sampling Continuos Signals
3/30
Figure 1.2. Simulation of the sampled version at rates (a) little above the
Nyquist rate, and (b) using the oversampling technique.
Exercise 1.1.- Sample a sinusoid (Fo = 2 kHz), sample the analog signal using
oversampling (Fs1 = 10 kHz), and undersampling (Fs2 = 3 kHz), and plot the
result.
Figure 1.3. Simulation of the sampled version at rates for a sinusoid Fo = 2 kHz
(a) oversampling (10 kHz) (b) undersampling technique (3 kHz).
-
8/11/2019 Sampling Continuos Signals
4/30
Task: In the above figure, the red signal (analog signal)is periodic with a Period
of T = 0.5 x 10, which means = 2 . Using the scope tool check theperiods and frequencies in the discrete time.
Experiment 1.2. In this experiment, we are going to explore the use of the
Simulink tool, which we are going to use along this practice together with the
Matlab. In this introduction, we are use the Sample and Hold. In this experiment
we are going summed up three sinusoid signals:
Fo1 = 500 Hz, A = 1 V.
Fo1 = 1500 Hz, A = 0.5 V.
Fo1 = 2500 Hz, A = 0.2 V.
The impulse generator feeds the Sampling and Hold Component. In another
words, this acts like an impulse train hitting the analog simulated signal. There
are three graphics; the first one is the Sinusoids Analog Signals. Which show us
the continuous three sinusoid that has been summed up. The plot of the Impulse
Samples, shows the impulses hitting in the analog signal. Finally, the Sample
and Hold Plot, figure out the sample and hold representation of the analog
signal.
In the figure 1.4. We can appreciate the experiment, and in the figure 1.5 we can
see the results from this example.
Figure 1.4. Simulation in Simulink: 3 sinusoids summed up, pass through the
sampled and hold
-
8/11/2019 Sampling Continuos Signals
5/30
Figure 1.5: Results from the Simulink model (a) Show the original simulation
(analog signal) (b) the signal been sampled at a frequency Fs = 8 KHz
Exercise 1.2.- Now to sample the signal, use an Zero-Order Hold from the
Discrete Library of the Simulink, setting the frequency sampling to 1/8000 (Fs =
8 kHz), and compare the result with the last example. There is any difference?
Experiment 1.3. In this example we are going to see the Spectral analysis in the
signal operating at = 2 , in this case we are going to do the samplingin = 5 , and review the spectrum of the signal. The codes that make thisexperiment generate the Figure 1.6.
% Espectral analisys
clc
clear allclose allFs = 5000; % sampling frequency% change sampling frequency to observe the effects of under
sampling% (Aliasing) and over samplingf = 2000; % signal frequency f Hz% change signal frequency and analyze the outputTmax = 10;t = 0:1/Fs:Tmax;s = 10*sin(2*pi*f*t) + rand(1,Tmax*Fs+1);Nfft = 2^8;[Pxx,freq] = pwelch(s,Nfft,[],[],Fs);%computing power spectral
densityplot(freq,Pxx), grid
-
8/11/2019 Sampling Continuos Signals
6/30
xlabel('frequency in hz');ylabel('power P')title('Signal PSD')
% frequency domain view% Compute the frequency response
w1=-Fs/2:Fs/1024:(Fs/2)-1/1024;S = fftshift(fft(s,1024));figureplot(w1,abs(S));gridxlabel('Frequency in hz');ylabel('Signal Amplitude A')title('Frequency Response')
Figure 1.6: FFT of the sampled signal, the = 5 , we are over samplingthe signal.
Exercise 1.3.- In this exercise change the frequency sampling to (a) =4 , and (b) = 3 , and see the results to this changes, answer if theresults correspond with the theory.
Exercise 1.4.- Now, using the example in the Experiment 1.2. put a BFFT to
explore the spectrum signal. The experiment should look like this one, in the
figure 1.7. Hint: use a buffer size, and FFT length of 262144.
-
8/11/2019 Sampling Continuos Signals
7/30
Figure 1.7: FFT analysis based on the experiment 1.2
Task: With the zoom scope analyze where the spectrum hits, and answer if this
correspond with the theory, change the Fs to do undersampling, and see what
happens with the spectrum.
Experiment 1.5. We are going to review the example with Aliasing, in this case
we plot the signal, in the right frequency sampling. The signal is cosine signal
= 60 , and then use a phase of /3. The signal is sampled at 400 Hz.
% Aliasing concept% Frequency in Fs = 400 Hzta = 0:1/4000:2/60; % analog time axis
xa1 = cos(2*pi*60*ta+pi/3);
tn = 0:1/400:2/60; % discrete-time axis as n*Tsxn1 = cos(2*pi*60*tn+pi/3);
plot(ta,xa1)hold onstem(tn,xn1,'filled', 'Color','red')axis([0 0.0333 -1.1 1.1])title('x(t) = cos(60t) + \pi/3 sampled in Fs = 400 Hz')
xlabel('n')ylabel('Amplitude')
-
8/11/2019 Sampling Continuos Signals
8/30
The result of this code is:
Figure 1.7: Cosine signal at an operation frequency in = 60 beensampled in = 400
Exercise 1.5- Based on the experiment 1.5. Now, you can create two another
signals this are the a cosine signal with a = 340 and a phase of =/3, and another cosine signal with a = 460 and a phase of =+/3. And then, plot the same discrete signal sampled at = 400 . Inanother words, the red signal is going to be the same xn1 of the above code
.Finally make the plot together with Hold Onto see the effect of the simulated
aliasing.
Task: Why the red discrete signal always beat with the analog simulated signal?
Experiment 1.6. In this experiment we are going to analyze the effect of the
aliasing with an audio signal. The experiment creates a signal that begins with a
frequency operation at = 200 , and this is going to be sampled at = 2 , as is obvious we are using oversampling. But in this case we aregoing to shift the frequency operation from 200 to reach2200 . With thisoperation if we maintain the frequency sampling, there is no more oversampling
to pass work under the Nyquist rate, and create the aliasing effect. Finally, we
create an audio signal to hear the effect of the aliasing. The code that makes this
project is presented below.
-
8/11/2019 Sampling Continuos Signals
9/30
clear all;close all;
%% Initialization
Fs = 2000; % Frequeny Smaplington_ds = 0.1; % Tone duration in seconds
N_samp = ton_ds*Fs; % Number of samplesn = [0:N_samp-1] / Fs; % n discre time vectorFs_cont = 10; % Fs Factor for continuous plot
(oversampling)t_const = [0:N_samp*Fs_cont-1] / (Fs*Fs_cont); % continuous time
vector
%% Figure configurationsset(gcf,'Position',[0 0 1023 768]);rect=get(gcf,'Position');
%% Input parameters to create the signals% We are going to create a sinusoidal signal with a begin frequency
% in Fo = 200 Hz, and this is going to shift until reach Fo = 2200 Hz% the amplitud is about A = 0.5; respectively.
% The idea is pass from low frequencies to the high frequency to see
% the spectrum and the effect of the aliasing. The sound of the signal% sampled properly and at low of the Nyquist point, to hear the effect
% of the aliasing.
f1 = 200; a1 = 0.5; % start parametersnum_plot_samples = 30; % Samples to plot the discrete signal
audio_out = []; % Empty array to fill the new created signal
frame_number = 0;forf_var = 0:10:2000
y = a1 * sin(2*pi*(f1+f_var)*n); % discrete signal
y_const = a1 * sin(2*pi*(f1+f_var)*t_const); % time constant sig
audio_out = [audio_out y];
stem(n(1:num_plot_samples),y(1:num_plot_samples)); hold on;
plot(t_const(1:num_plot_samples*Fs_cont),y_const(1:num_plot_samples*Fs
_cont),'red');set(gca,'ylim',[-1 1]);
title(strcat('y(n) = a1*sin(2*pi*n*',num2str(f1+f_var),') sampledat', num2str(Fs),' Hz'));
xlabel('n [s]');ylabel('y(n)');hold off;
frame_number = frame_number + 1;shift_frame(frame_number) = getframe(gcf,rect); %mantain the
signal moving to represnet
end;
wavwrite(audio_out,Fs,'sinus_aliasing.wav');
-
8/11/2019 Sampling Continuos Signals
10/30
The result is iterative, and if you run you can visually this effect. The final time
representation is showed in the figure 1.8.
Figure 1.8: Final representation of the signal been aliased because is under the
Nyquist sampling rate.
Finally, you can hear the effect of the aliasing phenomena, because is created the
audio signal of the tone been increased and decreased in the file, as the name
sinus_aliasing
Exercise 1.6- With the basis of the experiment 1.6. Now you have to build a
composed signal with frequencies= 200 , = 400 , and =400 . Setting this signals with amplitudes of = 0.5, 0.3 ! 0.2,respectively. Now create three plots, the first one is the time signal showing in
red the analog shifting signal, and in blue discrete samples showing the effect of
the discrete signal with aliasing. In the second plot, you could show the
spectrum sampled at frequency of = 20 , obviously is mean that is goingto be oversampling, and the spectrum is plotted in a right way. In the last plot,
show the spectrum of the aliasing. The signal could be similar to the shown inthe figure 1.9.
-
8/11/2019 Sampling Continuos Signals
11/30
Figure 1.9: Composed signal and their spectrum sampled at = 20 (middle), and showing the spectrum with the aliasing phenomena = 2 .
(last graphic)
Task: Create the audio signals sampled at = 2 and 20 , to hear thedifference, and realize the audio effect of the aliasing. Could you explain, what
is going on?
2.Band-Pass Sampling
If the signal is a bandpass signal with bandwidth B (Bandwidth is simply thedifference between the highest and lowest frequency present in the signal). In
order for a faithful reproduction and reconstruction of a bandpass analog signal
with bandwidthB. The signal should be sampled in a frequency sampling "#that is greater than or equal to twice the maximum bandwidth of the signal, it
means $ 2%.
For example, in the bandpass signal extends from &= 200 and '=300 , the bandwidth will be % = 100 . In this case the Nyquist rate isminimum around $ 600 , but we can use the bandpass, to sample in thedouble of the band, and now the $ 200 . As you can see is less even the
-
8/11/2019 Sampling Continuos Signals
12/30
highest frequency, and for that reason the bandpass sampling is called
undersampling.
In the figure 2.1, we can see the folding frequencies and aliasing zones, the zone
1 is a mirror image of the zone 2 (with frequency reversal). Similarly zone3 is a
mirror image of the zone 4, etc
Figure 2.1. Folding frequency and aliasing zones
Experiment 2.1: Consider an AM signal centered at carrier frequency 1MHz,
with two components offset by 10KHz 0.99 MHz and 1.01 MHz. So the AM
signal contains three frequency components at 0.99 MHz, 1 MHz and 1.01 MHz.
Our desire is to sample the AM signal. As with the usual sampling theorem
(baseband), we know that if we sample the signal at twice the maximumfrequency i.e Fs>=2*1.01MHz=2.02 MHz there should be no problem in
representing the analog signal in digital domain.
By the bandpass sampling theorem, we do not need to use a sampler running at
Fs>=2.02 MHz. Faster sampler implies more cost. By applying the bandpass
sampling theorem, we can use a slower sampler and reduce the cost of the
system. The bandwidth of the signal is 1.01MHz-0.99 MHz = 20 KHz. So, just
sampling at Fs>=2*20KHz=40KHz will convert the signal to digital domain
properly and we can also avoid using an expensive high rate sampler (if Fs >=2.02 MHz used according to baseband sampling theorem).
Lets set the sampling frequency to be Fs=120KHz (which is 3 times higher than
the minimum required sampling rate of 40KHz or oversampling rate =3). Now
we can easily find the position of the spectral components in the sampled output
by using the aliasing zone figure as given above. Since Fs=120 KHz, Fs/2 will
be 60KHz. So the zone 1 will be from 0 to 60 KHz, zone 2 -> 60-120KHz and
so on. The three spectral components at 0.99MHz, 1MHz and 1.01 MHz will fall
at zone 17 ( how? 0.99 MHz/60 KHz = 16.5 , 1MHz/60KHz = 16.67 and
1.01MHz/60KHz = 16.83 , all figures approximating to 17). By the aliasing zone
-
8/11/2019 Sampling Continuos Signals
13/30
figure, zone 16 contains a copy of zone 17, zone 15 contains a copy of zone 16,
zone 14 contains a copy of zone 15 and so on Finally zone 1 contains the copy
of zone 2 (Frequency reversal also exist at even zones). In effect, zone 1
contains a copy of zone 17. Since the original spectral components are at zone
17, which is an odd zone, zone 1 contains the copy of spectral components at
zone 17 without frequency reversal.
If we review the principle of the bandpass sampling through the figure 2.2. We
can see where the undersampling replica can beat.
Figure 2.2. Bandpass sampling: (a) original signal continuous spectrum (b)
sampled signal spectrum replications
Since there is no frequency reversal, in zone 1 and based on the figure 2.2, we
can compute where the discrete three components will be. For example for the
central frequency, we have to do successive rests from the ( = 1 ) 120 = **0 , and continuous do the rest to the zone 1, where the centralfrequency is located at 40 , by simply inspection the other components areat 30 and 50 .
Now, we are going to do the implementation based on the Simulink tool. First to
ensure the = 120 , dont cause aliasing, is needed to see the ratesallowed. Recall the formula from Lyons:
2 % $ - $2+ % + 1
-
8/11/2019 Sampling Continuos Signals
14/30
The index represents the zone where the replica spectrum will be. Theprogram to see the allowed band is quite simple:
fc = 1e6;
B = 20e3;m = input('Ingresa el ndice: ');a = (2*fc-B)/mb = (2*fc+B)/(m+1)
The design is shown in the figure 2.3. It consists in the sum of the three
components. After that, is multiplied with a oscillator represented by and
sinusoidal signal. There are three systems, the first one applies the Nyquiste rate
to sample the signal, the second one applies a bandpass filter to eliminate the
double sideband signal and then is oversampled to see of the spectrum is
presented. The third system is the system undersampled at a frequency =120 .
Figure 2.3.Simulink representation of the BandPass Sampling for the above
example.
The spectral result to check the frequencies response are shown in the figure 2.4.
-
8/11/2019 Sampling Continuos Signals
15/30
Figure 2.4.Spectrum response in the undersampling method to discretize an
analog bandpass signal.
Exercise 2.1. Based on the example in the exercise 2.1 compute the neededto undersampling the signal in & = 0. ), = 1 ), ' = 1.0 ),and make that the discrete spectrum beat at & = 0 , = 10 ,' = 20 .
3.Iterpolation and Reconstruction Filter
From the sampling theorem and the preceding examples, it is clear that if we
sample band-limited "# (analog signal) above its Nyquist rate, then we canreconstruct "#) from its samples . This reconstruction can be thought ofas a 2-step process:
-
8/11/2019 Sampling Continuos Signals
16/30
First the samples are converted into a weighted impulse train:
" 7-#8
9: 8= ; + 146" 7-# 3046" 7-# ;
Then the impulse is filtered through an ideal analog lowpass filter band-
limited to the 3
-
8/11/2019 Sampling Continuos Signals
17/30
-
8/11/2019 Sampling Continuos Signals
18/30
Figure 3.2. Three sampled version (a) oversampling, (b) at the Nyquist rate and
(c) subsampling signal
Now, our objective is based on the sampled signals above create the signal in
continuous time. For that we will take the first example (the oversampling
signal), and interpolate it to create the analog signal. The code to create the
interpolate version is given below, and the figure 3.3 shoes the result from theinterpolation:
%plot x(n) for T_s = 0.01 sec% x_a(t) = cos(20*pi*t);clc; close all;
T_s1 = 0.01; n1 = [0:100];
x1 = cos(20*pi*n1*T_s1);Fs1 = 1/T_s1;t = 0:0.001:1;
nTs1 = n1*T_s1;xa = x1*sinc(Fs1*(ones(length(n1),1)*t-nTs1'*ones(1,length(t))));subplot(2,1,1); func = stem(n1,x1); axis([-5 105 -1.2 1.2]);set(func,'markersize',2); xlabel('n','FontSize',12);title(['x(n) = cos(20{\pi}nT_s) for T_s = 0.01
sec'],'FontSize',12);ylabel('x(n)','FontSize',14);
subplot(2,1,2)plot(t,xa)xlabel('t in sec','FontSize',12);title(['Sinc interpolation for T_s = 0.01 sec'],'FontSize',12);
ylabel('y_a(n)','FontSize',14);
-
8/11/2019 Sampling Continuos Signals
19/30
Figure 3.2. Interpolated versing of the oversampled signal 100 , thereconstructed signal is about = 10
Exercise 3.1: Based onthe above experiment, create the interpolated version for
the sampling frequencies at E= 20 and at = 10 .
Experiment 3.2: Now, in this experiment we are going to create a composed
analog signal and then sampling the composed signal from oversampling to
reach undersample the same signal. Then we are going to interpolate, to
reproduce the original analog signal. In a first moment the signal is
reconstructed well because we are working over the Nyquist rate, and finally
when the operation frequency of the analog signal exceeds the sampling
frequency, obviously the reconstructed signal cant reconstruct the original
sampled version.
First we need the interpolate function, based on the sinc function. In the next
code is showed the code that allow us, work the samples to get a interpolated
version.
-
8/11/2019 Sampling Continuos Signals
20/30
functionf = sinc_interpolate(s,fs_in,fs_out) % s = sampled
signal, fs_in = sampling frequency of input, fs_out = sampling
frequency of output
N = length(s);length_in_seconds = N / fs_in;
output_length_in_samples = ceil(length_in_seconds * fs_out);
Mat = [];forn = 1:N
t = [0:output_length_in_samples-
1]/output_length_in_samples*length_in_seconds; t = t * fs_in - (n-1);Mat = [Mat; s(n)*sinc(t)];
end;
f = sum(Mat);
We are going to create three sinusoidal signals, the initial frequency are 200 , = 400 and = 600 , the amplitudes are: = 0.5,= 0.3 and = 0.2 respectively. The code, that makes the project ispresented below, and the result is depicted in the figure 3.3.
clear all;
close all;
%% Initialization
Fs = 2000; % Frequeny Smapling
ton_ds = 0.1; % Tone duration in seconds
N_samp = ton_ds*Fs; % Number of samples
n = [0:N_samp-1] / Fs; % n discre time vector
Fs_cont = 10; % Fs Factor for continuous plot (oversampling)
t_const = [0:N_samp*Fs_cont-1] / (Fs*Fs_cont); % continuous time vector
%% Figure configurations
set(gcf,'Position',[0 0 1023 768]);
rect=get(gcf,'Position');
%% Input parameters to create the signals
% We are going to sum three sinusoisal signals, and we in frequencies% from 2200 Hz, 2400 Hz, and 2800 Hz. The amplitudes are 0.5, 0.3, and
0.2
% respectively. The idea is pass from low frequencies to the high
% frequency to see the spectrum and the effect od the aliasing.
% The sound of the signal sampled properly and at low of th Nyquist
% point, to hear the effect of the aliasing.
f1 = 200; a1 = 0.5;
f2 = 400; a2 = 0.3;
f3 = 600; a3 = 0.2;
num_plot_samples = 30; % Samples to plot
num_plot_samples1 = 50; % Samples to plot
audio_out = []; % Empty array to fill the new created signal
frame_number = 0;
-
8/11/2019 Sampling Continuos Signals
21/30
forf_var = 0:10:2000
y1 = a1 * sin(2*pi*(f1+f_var)*n); % discrete signal 1
y2 = a2 * sin(2*pi*(f2+f_var)*n); % discrete signal 2
y3 = a3 * sin(2*pi*(f3+f_var)*n); % discrete signal 3
y = y1 + y2 + y3; % discrete signal.
y_const1 = a1 * sin(2*pi*(f1+f_var)*t_const); % time constant sig 1
y_const2 = a2 * sin(2*pi*(f2+f_var)*t_const); % time constant sig 2
y_const3 = a3 * sin(2*pi*(f3+f_var)*t_const); % time constant sig 3
y_const = y_const1 + y_const2 + y_const3; % time signal
%audio_out = [audio_out y];
subplot(2,1,1);
stem(n(1:num_plot_samples),y(1:num_plot_samples));
hold on;
plot(t_const(1:num_plot_samples*Fs_cont),y_const(1:num_plot_samples*Fs_c
ont),'red');
set(gca,'ylim',[-1 1]);
hold off;
title(strcat('y(n) = a1*sin(2*pi*n*',num2str(f1+f_var),') +
a2*sin(2*pi*n*',num2str(f2+f_var),') +
a3*sin(2*pi*n*',num2str(f3+f_var),'), sampled at ',num2str(Fs),' Hz'));
xlabel('n [s]');
ylabel('y(n)');
interpolated_samples =
sinc_interpolate(y(1:num_plot_samples),Fs,Fs*Fs_cont);
subplot(2,1,2);
stem(n(1:num_plot_samples),y(1:num_plot_samples));
hold on;
plot(t_const(1:length(interpolated_samples)),interpolated_samples,'red')
;
set(gca,'ylim',[-1 1]);
hold off;
title('Reconstruction of y from its samples using Shannon''s
theorem');
xlabel('n [s]');
ylabel('y2(n)');
audio_out = [audio_out interpolated_samples];
frame_number = frame_number + 1;shift_frame(frame_number) = getframe(gcf,rect);
end;
wavwrite(audio_out,Fs,'audio_out1.wav');
%movie2avi(my_movie,'sinus_aliasing.avi','compression','indeo5','fps',10
);
-
8/11/2019 Sampling Continuos Signals
22/30
Figure 3.3. (a) Original signal, and the samples with aliasing effect, (b)
interpolated reconstruction of the signal.
Exercise 3.2: Based on the experiment 3.2, try to reconstruct the signal raising
the frequency sampling * , and interpolate the signal to get thereconstructed signal. In this case if we are using oversampling, it means that the
reconstructed interpolate signal have to be very similar to the original version.
Finally, create the audio signal for the original composed three tones and the
interpolated version and compare the results.
Experiment 3.3: This project consists in input a recorded audio signal, and
explore the downsample by a factor of 4, and after that interpolate the signal. In
this case we are going to interpolate by repeat the samples. The code that
implements this project is shown below.
-
8/11/2019 Sampling Continuos Signals
23/30
% The project consists in input a recorded audio signal, and
explore
% the downsample by a factor of 4.
% You have to record various audio signal
% with different frequency sampling
% Fs = (8000, 11025, 22050, 44100)
% In this first example you can run with a sample recorded% in the "audios" file
%% Audio read from previous recorded audio.
[x,fs]= wavread('audios\sonido');
% first shorten the sequence so that the length is multiple of 4
orig_sig=length(x);
N=floor(orig_sig/4)*4; % Signal multiple of 4
x = x(1:N); % Number of samples
disp([fs,N]); % display the frequency Sampling and the number of
samples.
%
%% Play the input signal
sound(x,fs); %orginal input signal
figure(1);
subplot(1,2,1)
bar(x(2000:2060),0.02);
axis tight;title('Waveform of Original Sound');
subplot(1,2,2)
psd(x,256,fs);
axis([0,fs/2,-80,0]);
xlabel('Hz');
ylabel('dB');
title('Spectrum of Original');
pause(2)
%% Downsample without prefiltering: take every 4th sample
y=x(1:4:N); %Take a sample ever 4 times
sound(y,fs/4); % hear the downsampled signal
figure(2);
subplot(1,2,1)
bar(y(2000/4:2060/4),0.02); %downsampled signal in time
axis tight;
title('Waveform Downsampled by 4');
subplot(1,2,2)
psd(y,256,fs/4); % spectral characteristic of the dow4
axis([0,fs/2,-65,0]);
xlabel('Hz');
ylabel('dB');
title('Spectrum of Down4');
pause(2);
%% Interpolation by repeating each sample 4 times
z=zeros(N,1); % Initialization vector
% interpolate by first copy the samples in x to every 4th
sample
% to z starting from 1st sample then starting at 2nd sample, and
so on
z(1:4:N) = y;z(2:4:N) = y;
-
8/11/2019 Sampling Continuos Signals
24/30
z(3:4:N) = y;
z(4:4:N) = y;
z1 = z';
sound(z1,fs);
figure(3);
subplot(1,2,1)bar(z1(2000:2060),0.02);
axis tight;
title('Waveform Upsampling by 4');
subplot(1,2,2)
psd(z1,256,fs);
axis([0,fs/2,-60,0]);
xlabel('Hz');
ylabel('dB');
title('Spectrum of Up4');
wavwrite(z1,fs,'audios_out\sonido_out');
%
%% Calculate the MSE
D = x - z1;
MSE1 = mean(D.^2);
MSE = 20*log10(MSE)
fprintf('\n Error between original and interpolated =
%g\n\n',MSE )
The result of this project is show in the figure 3.4. Where it shows the original
signal and their spectrum, the figure 3.5 shows the downsampled version by a
factor of 4. The figure 3.6 shows the interpolated version by repeat the samples
each four samples to reconstruct the original version.
Figure 3.4. (a)Part of the original signal in time (b) spectrum of the originalsignal
-
8/11/2019 Sampling Continuos Signals
25/30
Figure 3.5. (a)Part of the downsampled signal in time (b) spectrum of the
downsampled signal by 4
Figure 3.6. (a)Part of the upsampled signal in time (b) spectrum of the
upsampled signal in time
-
8/11/2019 Sampling Continuos Signals
26/30
Exercise 3.3: Based on the experiment 3.3, now we are going to use a
reconstruction filter. First create the file based on the experiment 3.3. Used the
same input, and then read it, and save in the variable , after that play theoriginal audio, then short the sequence so that the length is multiple of 4, like in
the experiment.
The next step is decimate the signal by a decimation filter. Use the following
command
y=decimate(x,4,30,'FIR');
With the decimated signal, create the audio signal through the sound command
to hear the effect of the decimation filter.
The next step is plot the decimation signal with the spectrum. The last step is
interpolate the signal by the use of the FIR filter, we use the interp command
from the signal processingtoolbox. The code is show as follows:
[z,h_intp]=interp(y,4); % Interpolate by a factor of 4
With this new interpolated version, create the plot of the signal and the
frequency response, and create the audio signal to hear the difference. Use the
minimum square error in dB, to research the error between the original version
and the reconstructed version. This version is better that the experiment 3.3?
Task: Create new audio signals in different frequency sampling, and N bits,
then modulate the signals to arise to a high frequency multiplying by an oscillate
signal and make the example of decimate and interpolate. The example to record
a new audio is
% r = audiorecorder(22050, 16, 1);
% record(r); % speak into microphone...% pause(r);% p = play(r); % listen% resume(r); % speak again% stop(r);% p = play(r); % listen to complete recording% mySpeech = getaudiodata(r, 'int16'); % get data as int16
array
Fs = 22050;y = wavrecord(5*Fs, Fs, 'int16');wavplay(y, Fs);wavwrite(y,Fs,16,'audios\sonido3.wav')
-
8/11/2019 Sampling Continuos Signals
27/30
4.PCM (Pulse Code Modulation)
PCM was first developed by Alec Reeves, a British Engineer in 1937. It is an
analog signal that's sampled at uniform intervals and converted to a digital
binary number. The technology was originally developed for telephone
communication system. However, today is also being used with other digital
audio formats such as audio CD's, DVD's, and Blu-Ray discs.
Figure 4.1. Pulse Code Modulation Signal representation.
Above is a basic graphic example of what occurs during the pulse code
modulation process. The blue line is an example of an audio signal, or sine
wave, and the jagged red line would be digital representation of that line. In this
example, the sampling points would be 0, 3, 6, 8, 10, 11, 12, 12, 12, 11, 10, 8, 6,
4, and 0 or in binary: 00000000 00000011 00000110 00001000 00001010
00001011 00001100 00001100 00001100 00001011 00001010 00001000
00000110 00000100 00000000.
Experiment 4.1: In the following we made the PCM representation, from a
sinusoidal signal, we made a uniform quantization and after that we did the
binary representation using just 2 and 3 bits. Finally the representation is showed
in the figure 4.2. The binary data can be saved in a excel sheet or it are showed
in the command pronto pf the Matlab
-
8/11/2019 Sampling Continuos Signals
28/30
% Conversin analogica digital en Matlab
A = input('Input the amplitude: '); % Amplitude
fo = input('Input the frequency: '); % Frrequency operation
p = input('Input the phase: '); % Phase
n = input('Input the number of samples by period: '); % N
b = input('Input the number of bits: ');
disp('Para archivo Excel')
file = input('\n\nWhich is the file name: ', 's');
hoja = input('Sheet where you want to work: ', 's');
desc =[{'N of sampes'},{'Time in sec.'},{'Voltage'},{'V. Binary'}]
T = 1/fo; % period
Tm = T/n; % sample time
switchb
case2 % case represent two bit to quantize
forx = 1:n % cicle / start in 1, increas 1 by 1, until
t = Tm*x;
y = (A/2)*sin(2*pi*fo*t+p) + (A/2);
ify > 0 && y (A/4) && y (A/2) && y (3*A/4) && y 0 && y (A/8) && y (2*A/8) && y (3*A/8) && y (A/2) && y
-
8/11/2019 Sampling Continuos Signals
29/30
fprintf('%g %f %f %s \n\n',x,t,y,f)
elseify > (5*A/8) && y (3*A/4) && y (7*A/8) && y
-
8/11/2019 Sampling Continuos Signals
30/30
Exercise 4.1: Based on the exampleshowed in the experiment 4.1, increment
the number of bits from 3 to 5 bits, and plot the binary representation of the
signal.