Lab 2: Sinusoidal Signals, Complex Exponentials, and Manipulation of Audio Signals ELG3125 A – Signal and System Analysis Fall 2017 School of Engineering and Computer Science University of Ottawa Professor: Martin Bouchard Group Members: Duc Duy Pham, 7812628 Vinh-Khiem Justin Tran, 8199665 Experiment Date: September 26th, 2017 Report Due Date: October 10th, 2017 Table of Contents 1.0 Objectives.................................................................................................................................1 2.0 Reading and Writing Audio Files...........................................................................................1 2.1 Results of Reading and Writing Audio Files..........................................................................1 3.0 Sinusoidal Signals and Periodicity.........................................................................................2 3.1 Results of Sinusoidal Signals and Periodicity........................................................................2 4.0 Complex Exponential Signals.................................................................................................8 4.1 Results of Complex Exponential Signals...............................................................................8 Appendix A: Reading and Writing Audio Files Matlab Source Code....................................10 Appendix B: Sinusoidal Signals and Periodicity Matlab Source Code...................................11 Appendix C: Complex Exponential Signals Matlab Source Code..........................................17 ii 1.0 Objectives The objectives as stated in the lab manual is, “… to learn to read, listen to, and write audio files from Matlab, and to experiment with the properties of sinusoidal signals and complex exponentials in continuous time and discrete time.” 2.0 Reading and Writing Audio Files In this part of the lab, one will learn how to listen to a generated wave. They will then learn how to write the wave to a sound file and then read from the sound file. The read sound wave will then be verified against the original generated wave. 2.1 Results of Reading and Writing Audio Files The verification of the generated wave and the wave read from the audio file are shown in Figure 2.1, below. As can be seen, the two signals overlap each other on the plot. This proves that the wave that was read is a match of the generated wave. Figure 2.1. Comparison of generated wave and read wave. 1 3.0 Sinusoidal Signals and Periodicity In this part of the lab, one will learn about the different properties of continuous time and discrete time sinusoidal signals. 3.1 Results of Sinusoidal Signals and Periodicity Question 1 ω1 = π 2 ω2 =3 ω3 =√ 2 Figure 3.1. Comparison between three sinusoidal signals with different angular frequencies in continuous time. Regardless of the angular frequency, the sinusoidal signal in continuous time are always periodic. Question 2 π ω1 = ( Periodic ) 2 ω2 =3 ( Aperiodic ) ω3 =√ 2 ( Aperiodic ) 2 Figure 3.2. Comparison between three sinusoidal signals with different angular frequencies in discrete time. Sinusoidal signals in discrete time are not always periodic. Question 3 2 ω1 = 2 π=π 4 3 3 ω2 = 2 π= π 4 2 4 ω3 = 2 π=2 π 4 3 Figure 3.3. Comparison between three sinusoidal signals, in discrete time, which have the same period but are distinct (different values of m, constant values of N). While sinusoidal signals, in discrete time, can have the same period, they are distinct. Question 4 1 ω1 = ×2 π 5 ω2 =ω1 +2 π = 12 π 5 ω3 =ω1 +2 ×2 π= 22 π 5 Figure 3.4. Comparison between three sinusoidal signals, in discrete time, with a difference of multiple of 2 π in the angular frequencies One can add a multiple of 2 π in the angular frequency of a sinusoidal signal in discrete time and still get the same resulting signal. Question 5 low frequency , closer ω1=0.00123 ( ¿0 ) ω2 =1.55567 4 high frequency , closer ω 3=2.99978 ( ¿π ) Figure 3.5. Comparison between three sinusoidal signals in discrete time with near 0/near π angular frequency. In discrete time, the sinusoidal signals with frequencies near 0 generate the slowest change and those with frequencies near π generate the fastest change. Question 6 ω1 =π → T 1=2 ω2 =2 π →T 2=1 T 3 =2 ( which isthe LCM of 2∧1 ) 5 Figure 3.6.1. Addition of two sinusoidal signals in continuous time (with LCM). π ω1 = → T 1 =6 3 π ω2 = → T 2=4 2 T 3 =12 ( which isthe LCM of 6∧4 ) Figure 3.6.2. Addition of two sinusoidal signals in discrete time (with LCM). In either continuous time or discrete time, adding two sinusoidal signals will result in a sinusoidal signal whose period will be the LCM of the two original periods. 6 Question 7 ω1 =10→ T 1= π 5 π ω2 = → T 2=4 2 T 3 is indeterminate Figure 3.7. Addition of two sinusoidal signals in discrete time (no LCM). When the two original periods have no LCM, their resulting signal addition will not be periodic. In other words, it will have a infinite period. Question 8 ω1 =3 ( non− periodic ) π ω2 = ( periodic ) 2 7 Figure 3.8. Addition of non-periodic sinusoidal signal and periodic sinusoidal signal. In discrete time, adding a non-periodic sinusoidal signal with a periodic sinusoidal signal will result in a non-periodic one. 4.0 Complex Exponential Signals In this part of the lab, one will learn the effects of a positive frequency and a negative frequency on the result of a complex exponential signal. They will then verify that the real part of a complex exponential is a cosine wave and that the imaginary part of a complex exponential is a sine wave. 4.1 Results of Complex Exponential Signals The effects of positive frequency and negative frequency on complex exponentials are shown in Figure 4.1, below. As can be seen, a positive frequency on a complex exponential causes the resulting three-dimensional plot to form a corkscrew shape that is rotating clockwise. A negative frequency results in a corkscrew shape that is rotating counter clockwise. 8 Figure 4.1. Comparison of positive frequency and negative frequency on complex exponentials. The verification of the real part of a complex exponential representing a cosine wave and the imaginary part of a complex exponential representing a sine wave are shown in Figure 4.2, below. As can be seen, the expected results are indeed confirmed. 9 Figure 4.2. Verification of real and imaginary parts of complex exponential. 10 Appendix A: Reading and Writing Audio Files Matlab Source Code clear all clc % Create wave and play wave n=0:1:16000; y=sin(2*pi*0.1*n)+sin(2*pi*0.2*n); adjust=0.64983591643110114696039250089352; y1=adjust*y; Fs=8000; sound(y1,Fs) % Write wave to file filename='Lab2_Part1.wav'; audiowrite(filename,y1,Fs); % Read wave file clear Fs [z,Fs]=audioread(filename); % Display and playing audioread pause(1) figure subplot(2,1,1) stem(n(1:100),y1(1:100),'black') grid() title('Generated Wave') xlabel('n (samples)') ylabel('y[n]') subplot(2,1,2) stem(n(1:100),z(1:100),'yellow') grid() title('Read Wave') xlabel('n (samples)') ylabel('z[n]') sound(z,Fs) 11 Appendix B: Sinusoidal Signals and Periodicity Matlab Source Code Question 1 w1 = pi/2; w2 = 3; w3 = sqrt(2); t = 0:0.0001:8; y1 = sin(w1*t); y2 = sin(w2*t); y3 = sin(w3*t); %plot the 3 graphs figure subplot(3,1,1) plot(t,y1) grid on title('Sinusoidal Signal sin(pi/2t)') xlabel('t (s)') legend('sin(pi/2t)','Location','northeast') subplot(3,1,2) plot(t,y2) grid on title('Sinusoidal Signal sin(3t)') xlabel('t (s)') legend('sin(3t)','Location','northeast') subplot(3,1,3) plot(t,y3) grid on title('Sinusoidal Signal sin(sqrt(2)t)') xlabel('t (s)') legend('sin(sqrt(2)t)','Location','northeast') Question 2 w1 = pi/2; w2 = 3; w3 = sqrt(2); n = -8:1:8; y1 = sin(w1*n); y2 = sin(w2*n); y3 = sin(w3*n); %plot the 3 graphs figure subplot(3,1,1) stem(n,y1) grid on title('Sinusoidal Signal sin(pi/2n)') xlabel('n (samples)') 12 legend('sin(pi/2n)','Location','northeast') subplot(3,1,2) stem(n,y2) grid on title('Sinusoidal Signal sin(3n)') xlabel('n (samples)') legend('sin(3n)','Location','northeast') subplot(3,1,3) stem(n,y3) grid on title('Sinusoidal Signal sin(sqrt(2)n)') xlabel('n (samples)') legend('sin(sqrt(2)n)','Location','northeast') Question 3 w1 = pi; w2 = 3/2*pi; w3 = 2*pi; n = -8:1:8; y1 = sin(w1*n); y2 = sin(w2*n); y3 = sin(w3*n); %plot the 3 graphs figure subplot(3,1,1) stem(n,y1) grid on title('Sinusoidal Signal sin(pi*n)') xlabel('n (samples)') legend('sin(pi*n)','Location','northeast') subplot(3,1,2) stem(n,y2) grid on title('Sinusoidal Signal sin(3/2*pi*n)') xlabel('n (samples)') legend('sin(3/2*pi*n)','Location','northeast') subplot(3,1,3) stem(n,y3) grid on title('Sinusoidal Signal sin(2*pi*n)') xlabel('n (samples)') legend('sin(2*pi*n)','Location','northeast') Question 4 w1 =(1/5)*2*pi; w2 = w1+2*pi; w3 = w2+2*pi; 13 n = -8:1:8; y1 = sin(w1*n); y2 = sin(w2*n); y3 = sin(w3*n); %plot the 3 graphs figure subplot(3,1,1) stem(n,y1) grid on title('Sinusoidal Signal sin((1/5)*2*pi*n)') xlabel('n (samples)') legend('sin((1/5)*2*pi*n)','Location','northeast') subplot(3,1,2) stem(n,y2) grid on title('Sinusoidal Signal sin((w1+2*pi)*n)') xlabel('n (samples)') legend('sin((w1+2*pi)*n)','Location','northeast') subplot(3,1,3) stem(n,y3) grid on title('Sinusoidal Signal sin((w2+2*pi)*n)') xlabel('n (samples)') legend('sin((w2+2*pi)*n)','Location','northeast') Question 5 w1 = 0.00123; w2 = 1.55567; w3 = 2.99978; n = -8:1:8; y1 = sin(w1*n); y2 = sin(w2*n); y3 = sin(w3*n); %plot the 3 graphs figure subplot(3,1,1) stem(n,y1) grid on title('Sinusoidal Signal sin(0.00123n)') xlabel('n (samples)') legend('sin(0.00123n)','Location','northeast') subplot(3,1,2) stem(n,y2) grid on title('Sinusoidal Signal sin(1.55567n)') xlabel('n (samples)') legend('sin(1.55567n)','Location','northeast') 14 subplot(3,1,3) stem(n,y3) grid on title('Sinusoidal Signal sin(2.99978n)') xlabel('n (samples)') legend('sin(2.99978n)','Location','northeast') Question 6 Continuous w1 = pi; w2 = 2*pi; t = -8:0.00001:8; y1 = sin(w1*t); y2 = sin(w2*t); y3=y1+y2; %plot the 3 graphs figure subplot(3,1,1) plot(t,y1) grid on title('Sinusoidal Signal sin(pi*t)') xlabel('t (s)') legend('sin(pi*t)','Location','northeast') subplot(3,1,2) plot(t,y2) grid on title('Sinusoidal Signal sin(2*pi*t)') xlabel('t (s)') legend('sin(2*pi*t)','Location','northeast') subplot(3,1,3) plot(t,y3) grid on title('Sinusoidal Signal y3=sin(pi*t)+sin(2*pi*t)') xlabel('t (s)') legend('y3=sin(pi*t)+sin(2*pi*t)','Location','northeast') Discrete w1 = pi/3; w2 = pi/2; n = -20:1:20; y1 = sin(w1*n); y2 = sin(w2*n); y3=y1+y2; %plot the 3 graphs figure 15 subplot(3,1,1) stem(n,y1) grid on title('Sinusoidal Signal sin(pi/3*n)') xlabel('n (samples)') legend('sin(pi/3*n)','Location','northeast') subplot(3,1,2) stem(n,y2) grid on title('Sinusoidal Signal sin(pi/2*n)') xlabel('n (samples)') legend('sin(pi/2*n)','Location','northeast') subplot(3,1,3) stem(n,y3) grid on title('Sinusoidal Signal y3=sin(pi/3*n)+sin(pi/2*n)') xlabel('n (samples)') legend('y3=sin(pi/3*n)+sin(pi/2*n)','Location','northeast') Question 7 w1 = 10; w2 = pi/2; n = -50:1:50; y1 = sin(w1*n); y2 = sin(w2*n); y3=y1+y2; %plot the 3 graphs figure subplot(3,1,1) stem(n,y1) grid on title('Sinusoidal Signal sin(10n)') xlabel('n (samples)') legend('sin(10n)','Location','northeast') subplot(3,1,2) stem(n,y2) grid on title('Sinusoidal Signal sin(pi/2*n)') xlabel('n (samples)') legend('sin(pi/2*n)','Location','northeast') subplot(3,1,3) stem(n,y3) grid on title('Sinusoidal Signal y3=sin(10n)+sin(pi/2*n)') xlabel('n (samples)') legend('y3=sin(10n)+sin(pi/2*n)','Location','northeast') 16 Question 8 w1 = 3; w2 = pi/2; n = -20:1:20; y1 = sin(w1*n); y2 = sin(w2*n); y3=y1+y2; %plot the 3 graphs figure subplot(3,1,1) stem(n,y1) grid on title('Sinusoidal Signal sin(3n)') xlabel('n (samples)') legend('sin(3n)','Location','northeast') subplot(3,1,2) stem(n,y2) grid on title('Sinusoidal Signal sin(pi/2*n)') xlabel('n (samples)') legend('sin(pi/2*n)','Location','northeast') subplot(3,1,3) stem(n,y3) grid on title('Sinusoidal Signal y3=sin(3n)+sin(pi/2n)') xlabel('n (samples)') legend('y3=sin(3n)+sin(pi/2*n)','Location','northeast') 17 Appendix C: Complex Exponential Signals Matlab Source Code clear all clc % Base variables dt=0.01; t = 0:dt:3; fp=1; fn=-1; % Positive frequency exponential zp=exp(j*2*pi*fp*t); % Negative frequency exponential zn=exp(j*2*pi*fn*t); % Compare positive and negative frequnecy figure plot3(t,real(zp),imag(zp),'black'); hold on plot3(t,real(zn),imag(zn),'yellow'); hold off grid() title('Effects of Positive Frequency and Negative Frequency on Complex Exponentials') xlabel('t (s)') legend('Positive Frequency','Negative Frequency') % Check real = cos imag = sin figure subplot(2,1,1) plot(t,real(zp),'black') grid() title('Re(exp)=cos') xlabel('t (s)') ylabel('Re(exp)') subplot(2,1,2) plot(t,imag(zp),'yellow') grid() title('Im(exp)=sin') xlabel('t (s)') ylabel('Im(exp)') 18