Вы находитесь на странице: 1из 42

The University of Danang

Danang University of Science and Technology

Report Laboratory
EE 341
Instructor: Mạc Như Minh

Group members
Lê Quang Đạo
Võ Hoàng Chương
Nguyễn Công Thiện
LAB 1
ELEMENTARY MUSIC SYNTHESIS

1.1 – Music Synthesis

- Synthesize the piece of music appearing in Figure 2 using only information from Sections 2 and
3.
- Play it back using the SOUND command in Matlab. Type HELP SOUND for more
information. Please specify the sampling rate = 8k Hz in the playback.
- Save the entire music synthesis in an .m file. Include this .m file in your E-Submit.

Code from file EX1.m


Fs = 4000;

A = 220;
B = 220*2^(2/12);
C = 220*2^(3/12);
E = 220*2^(7/12);

t1 = 0:1/Fs:1;
t2 = 0:1/Fs:2;
t3 = 0:1/Fs:4;

%Create needed notes


yA1 = sin(2*pi*A*t1);
yA2 = sin(2*pi*A*t2);
yA3 = sin(2*pi*A*t3);
yB1 = sin(2*pi*B*t1);
yC1 = sin(2*pi*C*t1);
yE1 = sin(2*pi*E*t1);
P=zeros(1,700);
%Synthesize notes
y = [ yA2 P yA1 P yE1 P yE1 P yE1 P yB1 P yC1 P yB1 P yA3 ];
sound(y,8000);
1.2 – Volume Variations

- Improve the quality of the sound with a volume window function (see Section 4.1 above).
Try concatenating different function to model ADSR.
- Were you able to improve the sound quality? Save the modified music synthesis in a new m-file.
Include
this .m file in your E-Submit.
Code from file EX2.m

Fs = 4000;

A = 220;
B = 220*2^(2/12);
C = 220*2^(3/12); E
= 220*2^(7/12);

t1 = 0:1/Fs:1-1/Fs; t2
= 0:1/Fs:2-1/Fs; t3 =
0:1/Fs:4-1/Fs;

yA1 = sin(2*pi*A*t1);
yA2 = sin(2*pi*A*t2);
yA3 = sin(2*pi*A*t3);
yB1 = sin(2*pi*B*t1);
yC1 = sin(2*pi*C*t1);
yE1 = sin(2*pi*E*t1);

%Create ASDR Envelope


ADSR1 = ADSR(t1);
yADSR_A1 = yA1.*ADSR1;
yADSR_B1 = ADSR1.*yB1;
yADSR_C1 = ADSR1.*yC1;
yADSR_E1 = ADSR1.*yE1;

ADSR2 = ADSR(t2);
yADSR_A2 = ADSR2.*yA2;

ADSR3 = ADSR(t3);
yADSR_A3 = ADSR3.*yA3;

%Synthesize nodes
y = [ yADSR_A2 yADSR_A1 yADSR_E1 yADSR_E1 yADSR_E1 yADSR_B1 yADSR_C1
yADSR_B1 yADSR_A3 ];
sound(y,8000); plot(y);

Code from file ADSR.m (Supporting function for E2.m)


function [ y ] = ADSR( t )
levelA = linspace(0,1,length(t)/8);
levelD = linspace(1,0.75,length(t)/8);
levelS = linspace(0.75,0.75,length(t)/2);
levelR = linspace(0.75,0,length(t)/4);
y = [levelA,levelD,levelS,levelR];
end

Conclusion: The windowing function really improves the sound. It helps us to hear the sound more
fully.
1.3 – Tone Overlapping

- As explained in Section 4.2, allow the decaying notes (i.e. with the windowing function) to
overlap slightly in time. Decide the overlap duration yourself.
- Were you able to improve the sound quality? Save the modified music synthesis in a new .m
file. Include this .m file in your E-Submit.

Code from file EX3.m

%Create the tones


Fs = 4000;

A = 220;
B = 220*2^(2/12);
C = 220*2^(3/12);
E = 220*2^(7/12);

t1 = 0:1/Fs:1-1/Fs;
t2 = 0:1/Fs:2-1/Fs;
t3 = 0:1/Fs:4-1/Fs;

yA1 = sin(2*pi*A*t1);
yA2 = sin(2*pi*A*t2);
yA3 = sin(2*pi*A*t3);
yB1 = sin(2*pi*B*t1);
yC1 = sin(2*pi*C*t1);
yE1 = sin(2*pi*E*t1);

y = [ yA2 yA1 yE1 yE1 yE1 yB1 yC1 yB1 yA3 ];

%Create ASDR Envelope


ADSR1 = ADSR(t1);
yADSR_A1 = yA1.*ADSR1;
yADSR_B1 = ADSR1.*yB1;
yADSR_C1 = ADSR1.*yC1;
yADSR_E1 = ADSR1.*yE1;

ADSR2 = ADSR(t2);
yADSR_A2 = ADSR2.*yA2;

ADSR3 = ADSR(t3);
yADSR_A3 = ADSR3.*yA3;

%Create overlapping tones


tmp1=overlap(yADSR_A2,yADSR_A1);
tmp2=overlap(yADSR_A1,yADSR_E1);
tmp3=overlap(yADSR_E1,yADSR_E1);
tmp4=overlap(yADSR_E1,yADSR_B1);
tmp5=overlap(yADSR_B1,yADSR_C1);
tmp6=overlap(yADSR_C1,yADSR_B1);
tmp7=overlap(yADSR_B1,yADSR_A3);

%synthesize notes with overlapping tones


Y1=[yADSR_A2(1,1:end-499) tmp1 yADSR_A1(1,500:end-499) tmp2
yADSR_E1(1,500:end-499) tmp3 yADSR_E1(1,500:end-499) tmp3
yADSR_E1(1,500:end-499) tmp4 yADSR_B1(1,500:end-499) tmp5
yADSR_C1(1,500:end-499) tmp6 yADSR_B1(1,500:end-499) tmp7
yADSR_A3(1,500:end)];
sound(Y1,8000);
plot(Y1);

Code from file overlap.m (Supporting function for E2.m)

function [ y ] = overlap( x,z )


y=x(1,end-499:end)+z(1,1:500);
end

Conclusion:The function makes the sound more real and better


1

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
4
x 10

1.4 - Favorite Music Synthesis

Please repeat those assignments above with your favorite piece of music (around 15 notes).
Save this created piece of music in a new .m file. Include this .m file in your E-Submit.

Code from file EX4.m


%%%%%%%% Part 1 - Music Synthesis %%%%%%%
Fs = 4000;

%Assign the frequencies of the note


A = 220;
B = 220*2^(2/12);
C = 220*2^(3/12);
E = 220*2^(7/12);
G=220*2^(10/12);
Ac=440;
Cc=440*2^(3/12);

%Make length of the sound


t1 = 0:1/Fs:1-1/Fs; %fourth note
t2 = 0:1/Fs:2-1/Fs; %half note
t3 = 0:1/Fs:4-1/Fs; %whole note

%Create needed notes


N1=sin(2*pi*G*t1);
N2=sin(2*pi*Ac*t1);
N3=sin(2*pi*G*t2);
N4=sin(2*pi*E*t1);
N5=sin(2*pi*C*t1);
N6=sin(2*pi*Ac*t2);
N7=sin(2*pi*D*t2);
N8=sin(2*pi*Cc*t1);
N9=sin(2*pi*Cc*t2);

%%%%%%% Part 2 - Volume Variations %%%%%%%


%Create ASDR Envelope
ADSR1 = ADSR(t1);
ADSR2 = ADSR(t2);
x1=N1.*ADSR1; x2=N2.*ADSR1;
x3=N3.*ADSR2; x4=N4.*ADSR1;
x5=N5.*ADSR1; x6=N6.*ADSR2;
x7=N7.*ADSR2; x8=N8.*ADSR1;
x9=N9.*ADSR2;

%creating some overlapping tone


tmp1=overlap(x1,x2); tmp2=overlap(x2,x3);
tmp3=overlap(x3,x1); tmp4=overlap(x1,x4);

%Synthesize notes
Y2=[x1(1,1:end-499) tmp1 x2(1,500:end-499) tmp2 x3(1,500:end-499) tmp3
x1(1,500:end-499) tmp4 x4(1,500:end) x3 x5 x1 x2 x1 x3 x1 x2 x3 x1 x4
x6 x2 x2 x1 x4 x7 x1 x2 x3 x1 x4 x3 x1 x1 x8 x8 x6 x2 x8 x6 x8 x1 x6 x4 x1
x8 x8 x9];
sound(Y2,8000);
LAB 2
INTRODUCTION TO IMAGE PROCESSING

2.1. Edge Detection

RGB = imread('DailyShow','jpeg')
I = rgb2gray(RGB)
imshow(I)
size(I)
h1 = [-1 0 1; -2 0 2; -1 0 1]
h2 = [1 2 1; 0 0 0; -1 -2 -1]
M1 = conv2(im2double(I) , im2double(h1))
M2 = conv2(im2double(I) , im2double(h2))
figure()
imshow(abs(M1))
figure()
imshow(abs(M2))
figure()
imshow((M1.^2 + M2.^2).^0.5)

Grayscale image:
the row gradient magnitude:

the column gradient magnitude:


the overall gradient magnitude:

2.2. Scaling

First method
function output = ScaleSimple( RGB, factor )
rows = size(RGB,1);
columns=size(RGB,2);
new_img = [];
temp = [];
A =1;
for row = 1:factor:(rows-factor+1)
for column = 1:factor:(columns-factor+1)
if (mod(factor,2) == 0)
temp = [temp RGB( row , column )];
else if (mod(factor,2) == 1)
temp = [temp RGB(row + floor(factor/2),column + floor(factor/2))];
end
end
end
new_img(A,:) = temp;
A = A + 1;
temp = [];
end
output = uint8(new_img);
end
Scale=2

Scale=5

The Second method

function output = ScaleAdvance( RGB, factor )


rows = size(RGB,1);
columns=size(RGB,2);
temp = [];
new_img=[];
A =1;
for row = 1:factor:(rows-factor+1)
for column = 1:factor:(columns-factor+1)
temp = [temp sum(sum(RGB(row:1:(factor+row-1), column:1:(column+factor-
1))))/(factor.^2)];
end
new_img(A,:) = temp;
A = A + 1;
temp = [];
end
output = uint8(new_img);
end
Scale=2

Scale=5

The images from the second method seem to be better.

2.3. Image Flipping

RGB=imread('DailyShow','jpg');
I=rgb2gray(RGB);
updown=flipud(I);
subplot(3,1,1)
imshow(updown);
title('x[n, M-m+1]')
rightleft=fliplr(I);
subplot(3,1,2)
imshow(rightleft)
title('x[N-n+1, m]')
total=flipud(rightleft);
subplot(3,1,3)
imshow(total)
title('x[N-n+1, M-m+1]')
i. x[N-n+1, m]:

(ii) x[n, M-m+1]:


(iii) x[N-n+1, M-m+1]:

2.4. Image Expanding

Idea of this function is to enlarging the image by a factor of 2 using bilinear interpolation
Code sample:

RBG = imread('DailyShow','jpg');
origin = rgb2gray(RBG);
expanded = img_expand(origin);
figure
imshow(origin)
title('Original')
figure
imshow(expanded)
title('Expanded')
function y = img_expand( img )
rows = size(img,1);
columns=size(img,2);
new_img = [];
new_img(1:2:2*rows,1:2:2*columns) = img;
for row=1:1:(2*rows-1)
for column= 1:1:(2*columns-1)
if ((mod(row,2)==0) & (mod(column,2) == 1))
new_img(row, column) = (new_img(row-1,column) + new_img(row+1, column))/2;
end
if((mod(row,2)==1) & (mod(column,2) == 0))
new_img(row, column) = (new_img(row,column-1) + new_img(row, column+1))/2;
end
if((mod(row,2)==0) & (mod(column,2) == 0))
new_img(row, column) = (new_img(row-1, column-1) + new_img(row-1, column+1)
+ new_img(row+1, column -1) + new_img(row+1, column+1))/4;
end
end
end

y = uint8(new_img);
end
LAB 3
LINEAR CONVOLUTION
3.1. Image Expanding

- Find the output of each of the LTI systems which is convolution of y(n) = x(n)*h(n). Plot x(n), h(n) and y(n)
together, where x(n) = u(n-30) – u(n-50)

the system has the impulse response h1(n) = u(n - 10) - u(n - 20)
CODE FOR THIS EXERCISE:

t = 0:1:99;

x = [zeros(1,30) ones(1,70)] - [zeros(1,50) ones(1,50)]


h1 = [zeros(1,10) ones(1,90)] - [zeros(1,20) ones(1,80)]
h2 = (0.9.^t).* [ones(1,100)];
h3 = [1 -1 zeros(1,98)];

y1 = conv(x,h1);
y2 = conv(x,h2);
y3 = conv(x,h3);

subplot(3,1,1)
stem(t(1:100),x(1:100))
title('x')
subplot(3,1,2)
stem(t(1:100),h1(1:100))
title('h1')
subplot(3,1,3)
stem(t(1:100),y1(1:100))
title('y1')

figure()
subplot(3,1,1)
stem(t(1:100),x(1:100))
title('x')
subplot(3,1,2)
stem(t(1:100),h2(1:100))
title('h2')
subplot(3,1,3)
stem(t(1:100),y2(1:100))
title('y2')

figure()
subplot(3,1,1)
stem(t(1:100),x(1:100))
title('x')
subplot(3,1,2)
stem(t(1:100),h3(1:100))
title('h3')
subplot(3,1,3)
stem(t(1:100),y3(1:100))
title('y3')

3.2. Matrix-vector multiplication to perform linearconvolution

x = [1 2 3 4] and h = [3 2 1]

y = Hx

a/Determine the linear convolution y(n) = x(n)*h(n) :

y=[3 8 14 20 11 4]

b/ Express x(n) as a 4x1 column vector x and y(n) as a 6x1 column vector y. Now determine the 6x4
matrix H so
that y = Hx

2 3

x= 3 h= 2

4 1

H = y/x => H= 3 0 0 0

2 3 0 0

1 2 3 0

0 1 2 3

0 0 1 2

0 0 0 1
c/ Characterize the matrix H:

Each columns of H contains the h column vector and others numbers is 0. In matrix H, each descending

diagonal from left to right is constant.

Definition of Toeplitz matrix:

Toeplitz matrix is a matrix in which each descending diagonal from left to right is constant.
d/ What can you say about the first column and the first row of H?

The first row of H contains the first number in the h column vector.

The first column of H contains the h column vector and others numbers is 0.

e/ Develop an alternate Matlab function to implement linear convolution.

function[y, H]=EX2(h,x)
h=h';
x=x';
c=[h; zeros(1, length(x)-1)'];
r=[h(1) zeros(1,length(x)-1)];
H=toeplitz(c,r);
y=H*x;

3.3. z-transform and inverse z-transform

Apply z-transform:

Calculate the linear convolution y(n) = x(n)*h(n) where:

( ) ( )( ) [ ( )] ( )

And

( ) ( ) ( ) ( ) ( )
 Answer:

We have: ( ) and ( )

( ) ( ) ( ) ( ) ( ) ( )
( )

Using Matlab to compute the first 10 samples of the sequence y(n) corresponding to Y(z)

above: Result:

y= 0 0 0 0 -0.4167 -0.5556 -0.3935 -0.3395 -0.6600 -1.4700

Matlab code used for this exercise:

BY = [0 0 0 0 -5/12 5/6 -5/48];


AY = [1 -10/3 15/4 -8/3 55/48 -5/16 1/24];
[r,p,k] = residuez(BY,AY)
delta = [1 0 0 0 0 0 0 0 0 0];
y = filter(BY,AY,delta)
3.4. Circular convolution

x(n) = u(n-30) – u(n-50)


Report for Laboratory
EE341

Code used for this exercise:


n=0:1050;
x=[zeros(1,30) ones(1,21) zeros(1,1000)];
h1=[zeros(1,10) ones(1,11) zeros(1,1030)];
h2=(0.9.^n).*ones(1,length(n));
h3=[1 -1 zeros(1,1049)];

X=fft(x);
H1=fft(h1);
H2=fft(h2);
H3=fft(h3);

Y1=X.*H1;
Y2=X.*H2;
Y3=X.*H3;
y1=ifft(Y1);
y2=ifft(Y2);
y3=ifft(Y3);

figure;
subplot(3,1,1);
stem(n(1:100),x(1:100));
xlabel('x');
subplot(3,1,2);

22
Report for Laboratory
EE341
stem(n(1:100),h1(1:100));
xlabel('h1');
subplot(3,1,3);
stem(n(1:100),y1(1:100));
xlabel('y1');

figure;

subplot(3,1,1);
stem(n(1:100),x(1:100));
xlabel('x');
subplot(3,1,2);
stem(n(1:100),h2(1:100));
xlabel('h2');
subplot(3,1,3);
stem(n(1:100),y2(1:100));
xlabel('y2');

figure; subplot(3,1,1);
stem(n(1:100),x(1:100));
xlabel('x');
subplot(3,1,2);
stem(n(1:100),h3(1:100));
xlabel('h3');
subplot(3,1,3);
stem(n(1:100),y3(1:100));
xlabel('y3');

23
Report for Laboratory
EE341

LAB 5
THE FFT AND THE DIGITAL FILTERING
5.1. Matlab function “fft”

 When f = 0.25
Before shifting we have:

n = 0:127;
f = 0.25;
x = 1 + cos(2*pi*f*n);
X = abs(fft(x));

stem(n,X);
ylabel('Magnitude of FFT');
xlabel('k samples');

Fig1: The magnitude before shifting

24
Report for Laboratory
EE341

After shifting by using the fftshift

X = fftshift(X);
stem(n,X);
ylabel('Magnitude of FFT');
xlabel('k samples');

Fig2: The magnitude plot after shifting

For the cases where f = 0.25 and f = 0.5 . Use your understanding of the relation between
discrete and continuous time to plot the magnitude of the Fourier Transform of the continuous time
signal that these correspond to, assuming that the sampling period is T= 10-4. Be sure to label the
frequency axis correctly and indicate whether you are plotting in radians or Hertz or normalized
frequency.

25
Report for Laboratory
EE341

 The frequency scale from -0.5 and extends to 0.5

F = [-N/2:N/2-1]/N;
stem(F,X);
ylabel('Magnitude of FFT');
xlabel('w(rad/s)');
title('Shifted DFT with a Hz frequency scale, plotted in normalized
frequency');

Fig3: The frequency scale from -0.5 to 0.5 (normalized)

26
Report for Laboratory
EE341

 The frequency scale begins at -1 and extends to 1(frequency in Hertz)

F1 = [-N/2:N/2-1]/(N/2);
stem(F1,X);
ylabel('Magnitude of FFT');
xlabel('Freq(Hz)');
title('Shifted DFT plotted in herts');

Fig4: The frequency scale begins at -1 and extends to 1(frequency in Hertz)

27
Report for Laboratory
EE341

 The frequency scale begins at -π to π (frequency in radians)


F2 = [-N/2:N/2-1]*2*pi/N;
stem(F2,X);
ylabel(‘Magnitude of FFT’);
xlabel(‘w(rad/s)’);
title('Shifted DFT with a Hz frequency scale, plotted in radians');

Fig 5: The frequency scale begins at -π to π (frequency in radians)

28
Report for Laboratory
EE341

 When f = 0.5

Fig 6: The magnitude of the FFT of the signal before fftshift

29
Report for Laboratory
EE341

Fig 7: The magnitude of the FFT of the signal after the fftshift

30
Report for Laboratory
EE341

 The frequency scale from -0.5 and to 0.5(normalized frequency)

Fig 8: The frequency scale from -0.5 and to 0.5(normalized frequency)

31
Report for Laboratory
EE341

 The frequency scale from -1 to 1(frequency in Hertz)

Fig 9: The frequency scale from -1 to 1(frequency in Hertz)

32
Report for Laboratory
EE341

 The frequency scale from -π to π (frequency in radians)

Fig 10: The frequency scale from -π to π (frequency in radians)

Discuss why the frequency peak locations make sense

Answer:

When f1 = 0.25 Hz → ω1 = π/2

f2 = 0.5 are and ω2 = π,

as these plots showed, their peaks are at the point π/2 and π location. There is also a peak at ω
= 0 that is the frequency for the constant part in the signal.

33
Report for Laboratory
EE341

5.2. Frequency Shifting

For each of the following sequences, let f1= sinc and 0 ≤ n ≤ 255. Use the built-in sinc
function in MATLAB.

Plot the magnitude and phase plots (using plot), where the magnitude and phase plots are
over the range −0.5 ≤ ω < 0.5

a. x1[n]= sinc(f1(n-32))

n = 0:225;
w = -0.5:(1/225):0.5;
f1 = 0.15;
x1 = sinc(f1*(n-32));
X1 = fft(x1);
X1 = fftshift(X1);

subplot(2,1,1);
plot(w,abs(X1));
title('signal1');
ylabel('Magnitude');
xlabel('w(rad/s)');

subplot(2,1,2);
plot(w,angle(X1));
ylabel('Phase');
xlabel('w(rad/s)');

34
Report for Laboratory
EE341

Fig 11:
Magnitude and phase plot of x1

b. x1[n]= sinc(f1(n-32))(-1)n
x2 = sinc(f1*(n-32)).*((-1).^n);
X2 = fft(x2);
X2= fftshift(X2);
subplot(2,1,1);
plot(w,abs(X2));
title('signal 2');
ylabel('Magnitude');
xlabel('w(rad/s)');
subplot(2,1,2);
plot(w,angle(X2));
ylabel('Phase');
xlabel('w(rad/s)');

35
Report for Laboratory
EE341

Fig 12: Magnitude and phase plot of x2

c. x1[n]= sinc(f1(n-32))cos(2πf2n) where f2=0.2

f2 = 0.2;
x3 = x1.*(cos(2*pi*f2*n));
X3 = fft(x3);
X3 = fftshift(X3);
subplot(2,1,1);
plot(w,abs(X3));
title('signal 3');
ylabel('Magnitude');
xlabel('w(rad/s)');
subplot(2,1,2);
plot(w,angle(X3));
ylabel('Phase');
xlabel('w(rad/s)');

36
Report for Laboratory
EE341

Fig 13:
Magnitude and phase plot of x3

d. x1[n]= sinc(f3(n-32))cos(2πf3n) where f3=0.4

F3 = 0.4;
x4 = sinc(f3*(n-32)).*(cos(2*pi*f3*n));
X4 = fft(x4);
X4 = fftshift(X4);
subplot(2,1,1);
plot(w,abs(X4));
title('signal 4');
ylabel('Magnitude');
xlabel('w(rad/s)');
subplot(2,1,2);
plot(w,angle(X4));
ylabel('Phase');
xlabel('w(rad/s)');

37
Report for Laboratory
EE341

Fig 14: Magnitude and phase plot of x4

By looking at these figures, we can obtain:


 Signal 1 is low pass filter
 Signal 2 is high pass filter
 Signal 3 is band pass filter
 Signal 4 is shaping pass filter
 Signal 4 in (d) does not have a flat frequency response in the passband because
DTFT of sinc function is rectangular perfectly when time indexes approach to
infinity but this value cannot be found in MATLAB.

38
Report for Laboratory
EE341

5.3. FIR (Finite Impulse Response) Digital Filters

Use the MATLAB function “sptool” to create a low pass FIR filter of order 10 (by playing
around the passband and stopband ripples specifications) with passband cutoff frequency of
0.3 and stopband cutoff frequency of 0.4 . Use frevalz01 to study the system. Turn in the
frevalz01 plot of the system responses. Save your filter in a vector since you’ll use it again in
part 3.

Using the frevalz01 to plot of the system responses (after exporting from the filter
above):

>> figure(1);

>> frevalz01(Num,[1]);

39
Report for Laboratory
EE341

40
Report for Laboratory
EE341

5.3. IIR (Infinite Impulse Response) Digital Filters

Conclusion:
The phase responses of IIR filter and FIR filter are different.

41

Вам также может понравиться