03.06.2014 Views

Sampling of Continuous Signals in MATLAB

Sampling of Continuous Signals in MATLAB

Sampling of Continuous Signals in MATLAB

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Sampl<strong>in</strong>g</strong> <strong>of</strong> <strong>Cont<strong>in</strong>uous</strong> <strong>Signals</strong> <strong>in</strong> <strong>MATLAB</strong><br />

All signals <strong>in</strong> <strong>MATLAB</strong> are discrete-time, but they will look like cont<strong>in</strong>uous-time<br />

signals if the sampl<strong>in</strong>g rate is much higher than the Nyquist rate.<br />

Example (1):<br />

We want to sample a function: x = s<strong>in</strong>(2 pi f t),<br />

where f = 2 kHz and plot the samples values on a graph.<br />

Let x1 be the signal sampled at 10 kHz. Let x2 be the signal sampled at 3 kHz.<br />

Solution:<br />

f = 2000;<br />

T = 1/f;<br />

tm<strong>in</strong> = 0;<br />

tmax = 5*T; % Plot 5 cycles<br />

dt1 = 1/10000;<br />

dt2 = 1/3000;<br />

t1 = tm<strong>in</strong>:dt1:tmax;<br />

t2 = tm<strong>in</strong>:dt2:tmax;<br />

x1 = s<strong>in</strong>(2*pi*f*t1);<br />

x2 = s<strong>in</strong>(2*pi*f*t2);<br />

subplot(211)<br />

stem(t1,x1);<br />

subplot(212)<br />

stem(t2,x2);<br />

1<br />

0.5<br />

0<br />

-0.5<br />

-1<br />

0 0.5 1 1.5 2 2.5<br />

1<br />

0.5<br />

0<br />

-0.5<br />

x 10 -3<br />

-1<br />

0 0.5 1 1.5 2 2.5<br />

x 10 -3<br />

Note:<br />

STEM Discrete sequence or "stem" plot.<br />

STEM(Y): plots the data sequence Y as stems from the x axis term<strong>in</strong>ated<br />

with circles for the data value. If Y is a matrix then each column is plotted as a<br />

separate series.


Example (2):<br />

We want to sample a function: x = s<strong>in</strong>(2 pi f t),<br />

where f = 2 kHz and display the samples values <strong>in</strong> a vector.<br />

Let x1 be the signal sampled at 10 kHz. Let x2 be the signal sampled at 3 kHz.<br />

Solution:<br />

% Sample the s<strong>in</strong>usoid x = s<strong>in</strong>(2 pi f t), where f = 2 kHz, and<br />

plot the sampled signals over the cont<strong>in</strong>uous-time signal.<br />

% Let x1 be the signal sampled at 10 kHz.<br />

% Let x2 be the signal sampled at 3 kHz.<br />

f = 2000;<br />

T = 1/f;<br />

tm<strong>in</strong> = 0;<br />

tmax = 5*T;<br />

%<strong>Sampl<strong>in</strong>g</strong> rate<br />

dt1 = 1/10000;<br />

dt2 = 1/3000;<br />

for t1 = tm<strong>in</strong>:dt1:tmax<br />

i=1;<br />

x1(i) = s<strong>in</strong>(2*pi*f*t1);<br />

i=i+1;<br />

end;<br />

disp(x1);<br />

for t2 = tm<strong>in</strong>:dt2:tmax;<br />

j=1;<br />

x2(j) = s<strong>in</strong>(2*pi*f*t2);<br />

j=j+1;<br />

end;<br />

disp(x2);


X1:<br />

Columns 1 through 7<br />

-0.0000 0.9511 0.5878 -0.5878 -0.9511 -0.0000 0.9511<br />

Columns 8 through 14<br />

0.5878 -0.5878 -0.9511 -0.0000 0.9511 0.5878 -0.5878<br />

Columns 15 through 21<br />

-0.9511 -0.0000 0.9511 0.5878 -0.5878 -0.9511 -0.0000<br />

Columns 22 through 26<br />

0.9511 0.5878 -0.5878 -0.9511 -0.0000<br />

X2:<br />

Columns 1 through 7<br />

-0.8660 -0.8660 0.8660 -0.0000 -0.8660 0.8660 -0.0000<br />

Column 8<br />

-0.8660


Example (3):<br />

We want to sample a function: x = s<strong>in</strong>(2 pi f t),<br />

where f = 2 kHz and plot the samples values on a graph conta<strong>in</strong><strong>in</strong>g the orig<strong>in</strong>al<br />

signal.<br />

Let x1 be the signal sampled at 10 kHz. Let x2 be the signal sampled at 3 kHz.<br />

Solution:<br />

% Sample the s<strong>in</strong>usoid x = s<strong>in</strong>(2 pi f t), where f = 2 kHz, and<br />

plot the sampled<br />

% signals over the cont<strong>in</strong>uous-time signal.<br />

% Let x1 be the signal sampled at 10 kHz.<br />

% Let x2 be the signal sampled at 3 kHz.<br />

f = 2000;<br />

T = 1/f;<br />

tm<strong>in</strong> = 0;<br />

tmax = 5*T;<br />

dt = T/100; % take 100 po<strong>in</strong>ts with<strong>in</strong> each cycle<br />

% for an accurate plot use sufficient po<strong>in</strong>ts with<strong>in</strong> one cycle<br />

(100,200,500…) Increas<strong>in</strong>g ‘dt’ will decrease the accuracy<br />

t = tm<strong>in</strong>:dt:tmax;<br />

x = s<strong>in</strong>(2*pi*f*t);<br />

dt1 = 1/10000;<br />

dt2 = 1/3000;<br />

t1 = tm<strong>in</strong>:dt1:tmax;<br />

t2 = tm<strong>in</strong>:dt2:tmax;<br />

x1 = s<strong>in</strong>(2*pi*f*t1);<br />

x2 = s<strong>in</strong>(2*pi*f*t2);<br />

subplot(211)<br />

plot(t,x,'r');<br />

hold on<br />

stem(t1,x1); % to display the samples on the orig<strong>in</strong>al plot<br />

subplot(212)<br />

plot(t,x,'r');<br />

hold on<br />

stem(t2,x2); % to display the samples on the orig<strong>in</strong>al plot


1<br />

0.5<br />

0<br />

-0.5<br />

-1<br />

0 0.5 1 1.5 2 2.5<br />

x 10 -3<br />

1<br />

0.5<br />

0<br />

-0.5<br />

-1<br />

0 0.5 1 1.5 2 2.5<br />

x 10 -3


<strong>MATLAB</strong> treatment <strong>of</strong> audio signals<br />

Record<strong>in</strong>g an audio signal via a microphone:<br />

y = audiorecorder<br />

After you create an audiorecorder object, you can use the methods listed below on<br />

that object.<br />

record(y) Starts record<strong>in</strong>g<br />

record(y,length) Records for length number <strong>of</strong> seconds.<br />

stop(y) Stops record<strong>in</strong>g<br />

pause(y) Pauses record<strong>in</strong>g<br />

resume(y) Restarts record<strong>in</strong>g from where record<strong>in</strong>g was paused.<br />

play(y) Creates an audioplayer, plays the recorded audio data,<br />

and returns a handle to the created audioplayer.<br />

Read<strong>in</strong>g an audio file .wav<br />

y = wavread(filename) loads a WAVE file specified by filename, return<strong>in</strong>g<br />

the sampled data <strong>in</strong> y.<br />

[y, Fs, nbits] = wavread(filename) returns the sample rate (Fs) <strong>in</strong> Hertz and<br />

the number <strong>of</strong> bits per sample (nbits) used to encode the data <strong>in</strong> the file.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!