Dsplab2 PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ORGANIZATION OF THE ISLAMIC COOPERATION (OIC)

Digital Signals Processing Lab. (EEE 4742)


Lab 2: (a) Digital Sound Processing

Recording sound and verifying the effect of Nyquist:

Start with an example:

Create a MATLAB function that records voice (“light” and “dark”) from microphone with
sampling frequency, saves as hello.wav and olleh.wav files in current directory. Also introduce a
pause in between the two records.

Solution:

function y = myrecord (Fs)

%Fs=sampling frequency

recObj= audiorecorder(Fs, 16, 1);


pause on
disp('Start speaking.')
recordblocking(recObj, 5);
disp('End of Recording.');
y = getaudiodata(recObj);
filename = 'light.wav';
audiowrite(filename, y, Fs)
pause off

recObj= audiorecorder(Fs, 16, 1);


pause on
disp('Start speaking.')
recordblocking(recObj, 5);
disp('End of Recording.');
y1 = getaudiodata(recObj);
filename = 'dark.wav';
audiowrite(filename, y1, Fs)
pause off
BRAINSTORMING:
Question: What is the effect of changing sampling frequency of recording (i.e. in
audiorecord function) at 2,000 (or 2 KHz) and at 12,000 (12 KHz)?
Ans. Fs>2Fo for proper signal reconstruction, according to Nyquist. So, very poor quality
will be at 2 KHz because of aliasing because usual voice has highest frequency of around
Prepared by: IRTEZA ENAN KABIR & Taslim Reza (IUT) Digital Signal Processing Lab
15 KHz. In fact 12 KHz sampling should also produce bad quality if high-pitched sounds
like “eeeeeeee” are made. But in here “Hello” is a low-pitched voice. So we have good
quality even at 12 KHz.

Lab Report Question: What is the effect of changing sampling frequency of writing
(i.e. in wavwrite function) at 2,000 (or 2 KHz) and at 12,000 (12 KHz) if recording was
done at 6KHz? Where is conversion of continuous to digital signal happening? How can
you relate downsampling/upsampling to the above example?

Application of “Correlation” in voice signal:
Theoretical Background: In signal processing, cross-correlation is a measure of
similarity of two series as a function of the displacement of one relative to the other.
The cross-correlation between two signals x(n) and y(n) is

For, continuous time signal, the summation should be replaced as an integration and
signals should be represented as x(t) and y(t).
Cross correlation is used to find where two signals match:
u(t) is the test waveform. v(t) contains u(t) with an unknown delay and added noise.
If we slide the v(t) signal along u(t) and find the correlation according to the formula, we can know how
much the signals match. Also, how much displacements are between the signals.

Use of correlation for voice signal:

 Use autocorrelation to find decision-making of voice signal. Your program


should display “Light on” when it hears “Hello” and display “Light off” when
it hears “Olleh”. Use your hello.wav and olleh.wav for
convenience.
function y = light_on_off
x1 = audioread ('light'); % reference voice
x2 = audioread ('dark'); % reference voice

recObj= audiorecorder(Fs, 16, 1);


disp('Start speaking.')
recordblocking(recObj, 5);
disp('End of Recording.');
y = getaudiodata(recObj);

%inputs new voice


[Coefficient1 Lag1] = xcorr (y, x1);
Max_corr1 = max (Coefficient1);
figure (1)
plot(Lag1, Coefficient1)
[Coefficient2 Lag2] = xcorr (y, x2);
Max_corr2 = max (Coefficient2);
figure (2)
plot(Lag2, Coefficient2)
if (Max_corr1 > Max_corr2)
disp ('Light On !')
else
disp ('Light Off !')
end
end

Lab Report Question:


1) The above program is independent to time delay. Why?
2) Write short note on autocorrelation and normalized cross correlation.

Lab 2: (b) Discrete Signal and Systems


1. Introduction:
Any discrete signal is normally denoted by x(n), in which the variable n is integervalued and
represents discrete instances in time. Therefore, it is also called a discretetime signal, which is a
number sequence and will be denoted by one of the following
notations:

where the up-arrow indicates the sample at time instances, n = 0


In MATLAB a finite-duration sequence is represented by a row vector of appropriate
values.
However, such a vector does not have any information about sample position n.
Therefore, a correct representation of x(n) would require two vectors, one each for x
and n. For example, a sequence x(n)
can be represented in MATLAB
>> n = [-3 -2 -1 0 1 2 3];
>> x = [ 3 2 -2 1 5 -3 6];
Generally, we will use the x-vector representation alone when the sample position
information is not required:
2. Some elementary discrete signals:
Impulse function:

function [x, n] = discrete_delta(n0,n1,n2)


% Generates x(n) = delta(n-n0); n1 <= n <= n2
%Impulse function at n0
n = [n1:n2];
x = [(n-n0) == 0]; %True=1, False= 0
% Note: Replaces one for loop, one if statement and 5
%% A brilliant elementary example of smart coding.
stem(n,x)
end

Unit step function:

function [x, n] = discrete_signal(n0,n1,n2)


n = [n1:n2];
x = (n-n0)>=0;
stem(n,x)
end

Lab Report Question:

Create a function named addition, which will take input


n0,n1,n2, n00,n11,n22
to create two unit step functions like the above written code
save two unit functions by x1 and y1 and add them and save it on
y2. Remember
the two unit step functions can be different in length and it
can even start
from negative value. Addition has to be done on exact discrete
points.
HINT: you can check the value of n1 and n11 and see which is
smaller and try
use zero padding to the start of signal which needs it. Again
check n11 and
n22 and do the same at the end of the needed signal.
3) Signal Energy Power: Sample code

Ex= sum ( x.* conj(x)) %first approach


Ex= sum ( abs(x.^2) ) %Second approach

Px = Ex / length (n) %Note Ex has to be only for the index n not infinite sum

You might also like