Report Lab EE 341 Final
Report Lab EE 341 Final
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
- 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.
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;
- 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);
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);
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.
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);
ADSR2 = ADSR(t2);
yADSR_A2 = ADSR2.*yA2;
ADSR3 = ADSR(t3);
yADSR_A3 = ADSR3.*yA3;
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
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.
%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
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:
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
Scale=5
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]:
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;
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')
x = [1 2 3 4] and h = [3 2 1]
y = Hx
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
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.
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;
Apply z-transform:
( ) ( )( ) [ ( )] ( )
And
( ) ( ) ( ) ( ) ( )
Answer:
We have: ( ) and ( )
( ) ( ) ( ) ( ) ( ) ( )
( )
Using Matlab to compute the first 10 samples of the sequence y(n) corresponding to Y(z)
above: Result:
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');
24
Report for Laboratory
EE341
X = fftshift(X);
stem(n,X);
ylabel('Magnitude of FFT');
xlabel('k samples');
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
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');
26
Report for Laboratory
EE341
F1 = [-N/2:N/2-1]/(N/2);
stem(F1,X);
ylabel('Magnitude of FFT');
xlabel('Freq(Hz)');
title('Shifted DFT plotted in herts');
27
Report for Laboratory
EE341
28
Report for Laboratory
EE341
When f = 0.5
29
Report for Laboratory
EE341
Fig 7: The magnitude of the FFT of the signal after the fftshift
30
Report for Laboratory
EE341
31
Report for Laboratory
EE341
32
Report for Laboratory
EE341
Answer:
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
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
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
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
38
Report for Laboratory
EE341
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
Conclusion:
The phase responses of IIR filter and FIR filter are different.
41