School of Engineering and Information Technology

Total Page:16

File Type:pdf, Size:1020Kb

School of Engineering and Information Technology

School of Engineering and Information Technology

ASSESSMENT COVER SHEET

Student Name Rajeev Subramanian Student ID S194677 Assessment Title Laboratory Exercise 3 report Unit Number and ENG 421 Digital Signal Processing Title Lecturer/Tutor Prof Friso De Boer Date Submitted 25/3/2014 Date Received Office use only KEEP A COPY Please be sure to make a copy of your work. If you have submitted assessment work electronically make sure you have a backup copy.

PLAGIARISM Plagiarism is the presentation of the work of another without acknowledgement. Students may use a limited amount of information and ideas expressed by others but this use must be identified by appropriate referencing.

CONSEQUENCES OF PLAGIARISM Plagiarism is misconduct as defined under the Student Conduct By-Laws. The penalties associated with plagiarism are designed to impose sanctions on offenders that reflect the seriousness of the University’s commitment to academic integrity.

I declare that all material in this assessment is my own work except where there is a clear acknowledgement and reference to the work of others. I have read the University’s Academic and Scientific Misconduct Policy and understand its implications.* http://www.cdu.edu.au/governance/documents/AcademicandScientificMisconductPolicyv 1.03Jan2011.pdf.

Signed Date 25/3/2014

* By submitting this assignment and cover sheet electronically, in whatever form you are deemed to have made the declaration set out above. Synthesis of Sinusoidal Signals

Aim

This lab helps to understand the synthesis of music with sinusoids. A musical note can be synthesized by one sinusoid (one frequency), although in real musical instruments one note may produce many frequencies. Familiarity is gained with the help of sampling and digital to analog conversion by producing sounds using sinusoids, and by using an envelope and harmonics which are introduced to the musical synthesis sound more natural.

Objective

The basic objective of this lab is to understand and synthesis musical notes respective to their frequencies and sinusoids. The synthesised waveforms are firstly summed, sampled and reconstructed, in order to increase the quality of the output voice. In this lab the synthesis of following signal will be done while using the different combinations of basic sinusoids. 1. Sine waves at a specific frequency played through a D/A converter.

2. Sinusoids that create a synthesized version of Fur Elise.

Results

The basic sinusoid for this lab is x(t)=A cos (wot + ø). The sample distribution vector is necessary for that A=100, wo=2π(1100) and ø=0. Using the sampling rate of 8000 samples per second and calculating the total number of samples of 2 seconds of time duration for basic sinusoid with the help of matlab command tt=(0:0.01:3), create a vector of numbers from 0 to 3. The sound can easily be listened using the sound() function. The second vector computation is done with values A=100, wo- =2π(1650) and ø=π/3. Assuming two vectors as x1 and x2 a new vector is created with the sound frequency difference of, xx=[x1 zeros(1,2000) x2].

Section 2.2

Question a)

Matlab code: clc fs = 8000; Z = 100; dur = 2; f = 1100; [x1,t] = sumcos(f, Z, fs, dur); soundsc(x1,fs);

Question b) Matlab code: clc fs = 8000; Z = 100; dur = 2; f = 1100; [x1,t] = sumcos(f, Z, fs, dur); f = 1650; Z = 100*exp(j*(pi/3)); [x2,t] = sumcos(f, Z, fs, dur); soundsc(x1,fs); soundsc(x2,fs); % sounds higher pitched xx = [x1' zeros(1,2000) x2']; soundsc(xx,fs) % heard a pause between signals % if it is 2000 points and 8000 sampling frequency, it is 1/4 of a second

Higher frequency results in higher pitch. xx = [x1 zeros(1,2000) x2]; When xx is listened as the output, there is a space-time difference between the first and the second signal played. This space in time is related to the sampling frequency.

Question c)

Matlab code: clc fs = 8000; Z = 100; dur = 2; f = 1100; [x1,t] = sumcos(f, Z, fs, dur); f = 1650; Z = 100*exp(j*(pi/3)); [x2,t] = sumcos(f, Z, fs, dur); soundsc(x1,fs); soundsc(x2,fs); xx = [x1' zeros(1,2000) x2']; soundsc(xx,16000) % It is been observed that higher sampling rate, higher pitch and shorter duration. % The sound heard is twice as fast and variation in the pitch. This is because % the sampling frequency used is twice as much as before. % We are changing the frequency - the pitch because we are changing what is played % in one second. % So when playing the sound, the sampling frequency used should be % within the order to get the correct frequency.

Doubling the sampling rate in the sound function causes the output sound played to be shorter in duration and higher pitched. Sampling rate, using the sound function, is the rate at which the data is played. If the sampling rate is increased, faster the data played and hence shorter the duration occurred. This also results in higher pitch as duration is related to frequency. Higher frequency results in higher pitch. It is important to use the same sampling frequency that created the data or Ts sampling period in order to play the data, as the data itself does not contain this information. Using a different sampling frequency as that of recorded results in an inaccurate reproduction of the original signal. Section 2.3

In the second part of the lab, it is required to establish a formula for tone function in which the key number and duration is passed as arguments. After passing arguments, the function should calculate frequency for specific time duration. Next is the challenge to add harmonics of a specific frequency. This challenge is completed by taking some samples from one tone of the whole Fur Elise song. At this instance, it is assumed that all the harmonics for other frequencies are same, which is not possible.

Question a)

Matlab code: clc fs = 8000; Z = 100; dur = 2; f = 440*2^((56-49)/12) [x1,t] = sumcos(f, Z, fs, dur); soundsc(x1,fs);

>> f =

659.2551 <<

Question b)

Matlab code:

%note.m function tone = note(keynum,dur) % NOTE Produce a sinusoidal waveform corresponding to a given piano key number % usage: tone = note(keynum,dur) % tone = the output sinusoidal waveform % keynum = the piano keyboard number of the desired note % dur = the duration ( in seconds) of the output note fs = 11025; f = 440*2^((keynum-49)/12); Z = 1; [x,t] = sumcos(f,Z,fs,dur); if keynum == 0 f = 0; y = zeros(1,length(x)); x = y'; end tone = x;

Question c)

In play_scale.m file, the tone function is called several times to calculate sinusoids of different frequencies and then forming them in a vector to calculate the sinusoids for base frequencies. Lastly, add the normal tone and base tone frequencies together and play them through sound() function. Matlab code: % play_scale.m clc keys = [40 42 44 45 47 49 51 52]; % C D E F G A B C % key #40 is middle-c dur = 0.25 * ones(1,length(keys)); fs = 11025; xx = zeros(1,sum(dur)*fs+1); n1 = 1; for kk= 1:length(keys) keynum = keys(kk); tone = note (keynum,dur(kk)); n2 = n1+length(tone)+-1; xx(n1:n2) = xx(n1:n2) + tone'; n1 = n2; end soundsc(xx,fs)

Section 3

Synthesis of music

Matlab code: % the song Fur Elise clc clear keys=[56 55 56 55 56 51 54 52 49 0 40 44 49 51 0 44 48 51 52 0 44 56 55 ... 56 55 56 51 54 52 49 0 40 44 49 51 0 44 52 51 49 0 51 52 54 56 47 57 56 ... 54 45 56 54 52 44 54 52 51 0 44 56 0 0 56 68 0 0 55 56 0 0 55 56 55 ... 56 55 56 51 54 52 49 0 40 44 49 51 0 44 48 51 52 0 44 56 55 56 55 56 51 54 52 ... 49 0 40 44 49 51 0 44 52 51 49]; dur = [.5 .5 .5 .5 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 ... .5 .5 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1.5 .5 .5 .5 ... 1.5 .5 .5 .5 1.5 .5 .5 .5 1 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 . 5 ... .5 .5 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 .5 .5 .5 .5 . 5 .5 ... 1 .5 .5 .5 .5 1 .5 .5 .5 .5 2]; b=[0 0 25 32 37 0 0 20 32 36 0 0 25 32 37 0 0 ... 0 25 32 37 0 0 20 32 36 0 0 25 32 37 0 28 35 40 0 0 ... 23 35 39 0 0 25 32 37 0 0 20 32 44 0 44 56 0 0 55 56 0 0 55 56 0 0 ... 0 25 32 37 0 0 20 32 36 0 0 25 32 37 0 0 0 ... 25 32 37 0 0 20 32 36 0 0 25 32 37 0]; bdur=[1 3 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1 ... 3 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 1.5 .5 .5 .5 .5 1 ... .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 1 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 .5 1 ... 3 .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5 1 3 ... .5 .5 .5 .5 1 .5 .5 .5 .5 1 .5 .5 .5 .5]; dur = dur*0.5; bdur = bdur*0.5; %keys = [40 42 44 45 47 49 51 52]; % C D E F G A B C % key #40 is middle-c %dur = 0.25 * ones(1,length(keys)); fs = 11025; xx = zeros(1,sum(dur)*fs+1); n1 = 1; for kk= 1:length(keys) keynum = keys(kk);

tone = note (keynum,dur(kk)); n2 = n1+length(tone)-1; xx(n1:n2) = xx(n1:n2) + tone'; n1 = n2; end %sound(xx,fs) yy = xx; fs = 11025; xx = zeros(1,sum(dur)*fs+1); n1 = 1; for kk= 1:length(b) keynum = b(kk); tone = note (keynum,bdur(kk)); n2 = n1+length(tone)-1; xx(n1:n2) = xx(n1:n2) + tone'; n1 = n2; end xx = xx+yy; soundsc(xx,fs);

Sumcos Function

The same code as of play_scale.m was used, twice, once for the treble and once for the bass. The two vectors are then added and played. The envelope function and harmonics were added inside the sumcos function, which is called from the note function.

Matlab code: %sumcos function function [xx,tt] = sumcos(f, Z, fs, dur) %SUMCOS Function to synthesize a sum of cosine waves % usage: % xx = sumcos(f, Z, fs, dur) % f = vector of frequencies % (these could be negative or positive) % Z = vector of complex exponentials: Amp*e^(j*phase) % fs = the sampling rate in Hz % dur = total time duration of signal % % Note: f and Z must be the same length. % Z(1) corresponds to frequency f(1), % Z(2) corresponds to frequency f(2), etc. %figure t = [0:1/fs:dur]; t = t(:); Z = Z(:); tf = t*f; dur = dur-dur/6; % this creates small period of nothing at the end - transition of keys t1 = 0.1*dur/4; % this is time peak = 1 t2 = 0.5*dur/4; % this is time duration stops, sustain starts h2 = 0.9; % heigh at which sustain starts t3 = 3.5*dur/4; % time sustain stops, release starts h3 = 0.75; % high at which release starts m = 1/t1; y = m*t - m*(t-t1).*(t>t1) - ((1-h2)/(t2-t1))*(t-t1).*(t>t1) + ((1-h2)/(t2- t1))*(t-t2).*(t>t2) - ((h2-h3)/(t3-t2))*(t-t2).*(t>t2) + ((h2-h3)/(t3-t2))*(t- t3).*(t>t3) - (h3/(dur-t3))*(t-t3).*(t>t3) +( h3/(dur-t3))*(t-dur).*(t>dur); m = 4/dur; t1 = dur/4; t2 = 2*dur/4; t3 = 2.5*dur/4; y = m*t - m*(t-t1).*(t>t1) - ((1/6)/(t2-t1))*(t-t1).*(t>t1) + ((1/6)/(t2-t1))*(t- t2).*(t>t2) - (((1-1/6)-0.8)/(t3-t2))*(t-t2).*(t>t2) + (((1-1/6)-0.8)/(t3- t2))*(t-t3).*(t>t3) - (0.8/(dur-t3))*(t-t3).*(t>t3) +( 0.8/(dur-t3))*(t- dur).*(t>dur); y = m*t - m*(t-t1).*(t>t1) - ((1/6)/(t2-t1))*(t-t1).*(t>t1) + ((1/6)/(t2-t1))*(t- t2).*(t>t2) - 0.05*(t-t2).*(t>t2) + 0.05*(t-t3).*(t>t3) - 10*(t-t3).*(t>t3); y = y.*(y>0);

%plot(t,y) xx = y.*( real( (exp(j*2*pi*t*f))*Z ) + 0.4*real( (exp(j*4*pi*t*f))*Z ) + 0.2*real( (exp(j*6*pi*t*f))*Z ) ); xx = y.*( real( (exp(j*2*pi*t*f))*Z ) + 0.4*real( (exp(j*pi/2)*exp(j*4*pi*t*f))*Z ) + 0.2*real( (exp(j*6*pi*t*f))*Z ) );

%xx = y.*( real( (exp(j*2*pi*t*f))*Z )+ 0.001*real((exp(j*4*pi*t*f))*Z ) +0.001*real( (exp(j*6*pi*t*f))*Z )+0.001*real( (exp(j*7*pi*t*f))*Z ) +0.35*real( (exp(j*8*pi*t*f))*Z ) ); %xx = y.*( real( (exp(j*2*pi*t*f))*Z ) ); tt = t; %plot(t,xx);

The idea of envelopes and harmonics were used in the sumcos function. The envelope was adjusted in order to achieve a slight pause after each note so that the amplitude would reach zero before the duration of the note had finished. This was obtained by the line; ‘dur = dur-dur/6’, leaving a gap of dur/6 at the end of zero amplitude.

Envelope Function

The last challenge was to make an envelope for every tone so that more likely original piano tone will be achieved. The parameters of the envelope can be changed by changing t1,t2,t3,h2,h3. The idea of using step functions was used to obtain a simple envelope. The vector produced here is based on time t. Therefore its length is the same as time t, making it simple to multiply the signal by the envelope function. Matlab code: dur = 0.25; t = [0:1/11025:dur]; x = 0.5*t; y = t - 1.05*(t-1).*(t>1) + .04*(t-1.5).*(t>1.5) - 2*(t-3).*(t>3) ; y = y.*(y>0);

%plot(t,y) %figure %y = (t>1) - (t>4); dur = dur-dur/6 t1 = 0.5*dur/4; % this is time peak = 1 t2 = 1*dur/4; % this is time duration stops, sustain starts h2 = 0.83 t3 = 3*dur/4; % time sustain stops, release starts h3 = 0.8 m = 1/t1; y = m*t - m*(t-t1).*(t>t1) - ((1-h2)/(t2-t1))*(t-t1).*(t>t1) + ((1-h2)/(t2- t1))*(t-t2).*(t>t2) - ((h2-h3)/(t3-t2))*(t-t2).*(t>t2) + ((h2-h3)/(t3-t2))*(t- t3).*(t>t3) - (h3/(dur-t3))*(t-t3).*(t>t3) +( h3/(dur-t3))*(t-dur).*(t>dur);

%y = m*t - m*(t-t1).*(t>t1) - ((1/6)/(t2-t1))*(t-t1).*(t>t1) + ((1/6)/(t2-t1))*(t- t2).*(t>t2) - (((1-1/6)-0.8)/(t3-t2))*(t-t2).*(t>t2) + (((1-1/6)-0.8)/(t3- t2))*(t-t3).*(t>t3) - (0.8/(dur-t3))*(t-t3).*(t>t3) +( 0.8/(dur-t3))*(t- dur).*(t>dur); %y = m*t - m*(t-t1).*(t>t1) - ((1/6)/(t2-t1))*(t-t1).*(t>t1) + ((1/6)/(t2-t1))*(t- t2).*(t>t2) - 0.05*(t-t2).*(t>t2) + 0.05*(t-t3).*(t>t3) - 50*(t-t3).*(t>t3); %y = m*t - (((5/6)/(t2-t1)))*(m).*(t-t1).*(t>t1) + ((1/6)/(t2-t1))*(m).*(t- t2).*(t>t2) - (t-t3).*(t>t3) ; %y = y.*(y>0); plot(t,y) title('Envelope function') xlabel('Time - seconds') ylabel('Amplitude')

>>dur =

0.2083 h2 =

0.8300 h3 =

0.8000<< Figure 1: Envelope Function obtained

Matlab code: % NOTE Produce a sinusoidal waveform corresponding to a given piano key % number % usage: tone = note(keynum,dur) % tone = the output sinusoidal waveform % keynum = the piano keyboard number of the desired note % dur = the duration ( in seconds) of the output note keynum =41 dur = 4 fs = 11025; f = 440*2^((keynum-49)/12); Z = 1; [x,t] = sumcoss(f,Z,fs,dur); x=x/max(x); plot(t,x) title('Piano key number 41 duration 4 seconds') xlabel('Time - seconds') ylabel('Amplitude') %AXIS([0 4 0 1]) %AXIS([0 0.1 -1.5 1.5]) if keynum == 0 f = 0; y = zeros(1,length(x)); x = y'; end tone = x; soundsc(x,11025);

<

41 dur =

4 >> Figure 2: Sinusoidal waveform of keynum 41

Conclusion

This lab helps to learn more about matlab programming and to know about the behaviour of different sinusoid signals. In fact, the idea of using harmonics and envelopes improves the quality of the sound. Without using the idea of envelopes, clicking noises could be heard from the transition from note to note, produced by the sudden change of amplitude. The note function has been modified and changed to add envelope and harmonics thus to create chords by adding the signal vectors of several notes. In brief, the harmonic calculation as well as the overall programming was a challenging job.

Recommended publications