You are on page 1of 15

SAINT LOUIS UNIVERSITY

SCHOOL OF ENGINEERING AND ARCHITECTURE


DEPARTMENT OF ELECTRONICS ENGINEERING

ECE 515 FL
ACTIVITY No. 4
AUDIO SYNTHESIS

SUBMITTED BY:
ACANTILADO, Michelle Charmaine Y.
OCAMPO, Cyrus Eurl L.
ECE 515FL 7:30 10:30 T H306

SUBMITTED TO:
ENGR. JACQUELINE C. FLORES

05 October 2017
ECE 515 FL
Digital Signal Processing Laboratory Activity 4: Audio Synthesis Using SCILAB

Activity No. 4
Audio Synthesis Using SCILAB

I. OBJECTIVES

At the end of the activity, the student must be able to:


1. Demonstrate the concepts of audio synthesis.
2. Use audio synthesis to generate a musical tune, periodic signals , and dual-tone multi-
frequency (DTMF) signals.

II. BACKGROUND

Audio or sound synthesis is the technique of generating sound, using electronic hardware
or software, from scratch. The most common use of audio synthesis is musical, where electronic
instruments are used to generate sound similar to a wide array of musical instruments. Audio
synthesis has many applications such as generation of unique sounds that are difficult to produce
acoustically, recreation of real-world instruments and sounds, and generation of ideal test signals.
Audio signal synthesis involves the use of computational algorithms to compute the samples of a
signal, at a specified sampling frequency, based on their mathematical models. Then, the samples
are sent as input to a DAC to produce the audio signal.
Generally, the samples are computed by evaluating a continuous-time model of the signal
at instants of time corresponding to the sampling points. For example, the equation for the
instantaneous values of a pure tone with a frequency of 10Hz and an amplitude of 1 volt is
x(t) = sin(2ft) = sin(20t). For a sampling frequency Fs=100 samples per second, the samples are
taken at 10ms intervals. Assuming the sampling starts at t=0, then the sampling instants would be
at 0, 10ms, 20ms, 30ms and so on. Thus, to generate the samples of the tone, the equation for x(t)
is evaluated at the sampling instants.
When using Scilab, the samples should have values between -1 and 1 to avoid signal
clipping that results to distorted reproduction.
SLU ECE 2
ECE 515 FL
Digital Signal Processing Laboratory Activity 4: Audio Synthesis Using SCILAB

III. LABORATORY ACTIVITY

Understand and then execute the following Scilab programs that synthesize some common
signals.
1. Tone Generation
The mathematical model for a tone is x(t) = Asin(2ft), where A is the amplitude or peak
value and f is the frequency in Hz.
To generate a 250Hz tone of 3 seconds duration
Fs=22050; //Fs - sampling frequency
f=250; //f - signal frequency;
t=0:1/Fs:3; //Generate the values for time t for 3 seconds
sine=sin(2*%pi*f*t); //Compute the samples for the 250Hz tone
playsnd(sine,Fs); //playback the 500Hz tone

To generate tones for different musical notes


Fs=22050;
t=0:1/Fs:0.25; //Generate the values for time t
//for a duration of 0.25 seconds
//Compute the samples for each of the notes
do=sin(2*%pi*261.63*t); //frequency of note middle do is 261.63Hz
re=sin(2*%pi*293.67*t);
mi=sin(2*%pi*329.63*t);
fa=sin(2*%pi*349.23*t);
so=sin(2*%pi*392*t);
la=sin(2*%pi*440*t);
ti=sin(2*%pi*493.9*t);
doh=sin(2*%pi*523.26*t);
tune1=[do re mi fa so la ti doh]; //Put together the samples of each of the
//notes to form the do-re-mi tune

SLU ECE 3
ECE 515 FL
Digital Signal Processing Laboratory Activity 4: Audio Synthesis Using SCILAB

playsnd(tune1,Fs); //Playback the tune

2. Bell Sound Generation


The bell sound is modeled as a tone with an exponentially decaying envelope given as
x(t) = e-ktsin(2ft). The rate of decay of the exponential envelop depends on the value of k such
that a high value of k would mean a faster rate of decay and the bell sound fades faster.
To generate the bell sound

Fs=11025;
f=500; //f -pitch of the bell sound;
t=0:1/Fs:1;
bell=exp(-25*t).*sin(2*%pi*f*t); //Generate the samples for the bell sound
clf;plot2d(t,bell);xgrid; //Plot the waveform of the bell sound
playsnd(bell,Fs); //Playback the bell sound

To generate a tune using the bell sound


Fs=22050;
t=0:1/Fs:0.25;
do=exp(-7*t).*sin(2*%pi*261.63*t);
re=exp(-7*t).*sin(2*%pi*293.67*t);
mi=exp(-7*t).*sin(2*%pi*329.63*t);
fa=exp(-7*t).*sin(2*%pi*349.23*t);
so=exp(-7*t).*sin(2*%pi*392*t);
la=exp(-7*t).*sin(2*%pi*440*t);
ti=exp(-7*t).*sin(2*%pi*493.9*t);
doh=exp(-7*t).*sin(2*%pi*523.26*t);
bell_tune1=[do re mi fa so la ti doh];
playsnd(bell_tune1,Fs);

SLU ECE 4
ECE 515 FL
Digital Signal Processing Laboratory Activity 4: Audio Synthesis Using SCILAB

3. Ringing Tone Generation


A ringing tone is a high frequency tone with a low frequency sine wave envelope and
mathematically modeled as ring(t) = sin(2f1t)sin(2f2t), where f1 is the frequency of the envelope
and f2 is the frequency of the tone.

To generate a 3-second 500Hz ringing tone


Fs=22050;
t=1:1/Fs:3;
ring=sin(2*%pi*10*t).*sin(2*%pi*500*t);
clf;plot2d(t,bell);xgrid;
playsnd(ring, Fs);

4. Random Noise Generation


Random noise is mathematically modeled as a set of random numbers with zero mean.
The built-in function rand( ) may be used to generate the random numbers.
To generate a 3-second random noise
Fs=22050;
noise=2*rand(1,3*Fs)-1; //Generate a row vector of random numbers whose
//values vary between -1 and 1
playsnd(noise, Fs);
To generate a noise contaminated tone, generate separately the samples for the tone and
the samples for the random noise. Then add the samples array-wise.
t=0:1/Fs:3; //Generate the samples for a 3-second
tone=sin(2*%pi*250*t); //250Hz tone
noise=2*rand(1,length(t))-1; //Generate the samples for the noise
noisy_tone=sine+noise; //Combine the tone and noise
noisy_tone=noisy_tone/(max(abs(noisy_tone))); //Normalize the noisy tone
playsnd(noisy_tone,Fs);

SLU ECE 5
ECE 515 FL
Digital Signal Processing Laboratory Activity 4: Audio Synthesis Using SCILAB

5. Periodic Signal Generation


A periodic signal is mathematically defined for one period only and this model is used to
generate the samples for any time instant.
Consider a periodic triangular wave with period T and amplitude A

described for one period as


x(t) = -A + (4A/T)t, 0 t T/2
= 3A - (4A/T)t, T/2 t T
The value of x(t) for any t 0 can be computed as x(t) = x(t-kT) with k=floor(t/T)..

The following SCILAB function generates the samples of the triangular wave given the
amplitude A, fundamental frequency f (in Hz), the sampling frequency Fs (in samples/second),
and the duration tdur of the triangular wave.
function [x]=trigen(A,f,Fs,tdur)
//Function that generates a triangular waveform
//where: A - amplitude
// f - fundamental frequency
// Fs - sampling frequency
// tdur - time duration
T=1/f; // T - period of the triangular wave
x=[];
for t=0:1/Fs:tdur
tcor = t- floor(t/T)*T; //reflect the time t to an equivalent time between 0 and T
if tcor >= 0 & tcor < (T/2) then
x_temp = -A +(4*A/T)*tcor;
SLU ECE 6
ECE 515 FL
Digital Signal Processing Laboratory Activity 4: Audio Synthesis Using SCILAB

end;
if tcor >= (T/2) & tcor <T then
x_temp = 3*A + (4*A/T)*tcor;
end
x = [x, x_temp];
end;
endfunction;
To generate a 500Hz triangular wave with amplitude values varying from -0,5 to 0.5
and for a duration of 5 seconds, the following command lines are used
x=trigen(0.5,500,8000,5);
playsnd(x,8000);

IV. LABORATORY EXERCISES

1. Open and run the Scilab program music_player_demo.sce. Understand the program. Edit the
program to synthesize and playback the musical piece Sonatina by Theodore Latour. (You
may have to research on how to read musical pieces to determine the notes!). The playback
should be in stereo such that the melody part is played as the right channel signal and the bass
part is played as the left channel signal. Save the SCILAB program as Sonatina_stereo.sce.

SLU ECE 7
ECE 515 FL
Digital Signal Processing Laboratory Activity 4: Audio Synthesis Using SCILAB

PROGRAM: //Sonatina treble part


music=[do q
//FREQUENCY TABLE (in Hz)
re q
do=261.63; dol=do/2; doh=2*do;
mi q
re=293.67; rel=re/2; reh=2*re;
fa q
mi=329.63; mil=mi/2; mih=2*mi;
so q
fa=349.23; fal=fa/2; fah=2*fa;
la q
so=392; sol=so/2; soh=2*do;
ti q
la=440; lal=la/2; lah=2*la;
doh q
ti=493.9; til=ti/2; tih=2*ti;
ti q
rest=0;
la q
ti q
so q
doh q
so q
mi q
//DURATION OF NOTES (in seconds)
so q
w=2.00;
fa q
h=1.00;
la q
q=0.5;
so q
e=0.25
fa q
s=0.125;
mi q
so q
function x=note_gen(f, td, Fs)
fa q
//function that generates the samples of a note
mi q
//f - frequency of the note
re h
//td - duration of the note
re h
t=0:1/Fs:td-1/Fs
re h
x=exp(-0.5*t).*sin(2*%pi*f*t);
rest h
endfunction
do q
re q
//MAIN PROGRAM
mi q
stacksize(10^8);
fa q
Fs=8000;
so q
la q
//Load the text file containing the notes
ti q
//The notes should be contained in the variable
music
doh q
//as a 2-column matrix. The first column contains
ti q
//the notes and the second column contains the
la q
duration
ti q
//of the notes
so q
//Sonatina bass
doh q
music=[dol w
so q
mil w
mi q
sol h
so q
fal h
fa q
mil w
la q
rel w
SLU ECE 8
ECE 515 FL
Digital Signal Processing Laboratory Activity 4: Audio Synthesis Using SCILAB

dol w so q
sol q fa q
fal q mi q
sol q so q
fal q fa q
sol q mi q
fal q re q
mil q fa q
rel q mi q
dol w re q
mil w do h
sol h rest h];
fal h
mil w [m,n]=size(music);
rel w tune=[];
dol w for k=1:m
f=music(k,1);
td=music(k,2);
sol h tune=[tune, note_gen(f,td,Fs)];
sol h end;
dol h treble=tune;
rest h];
[m,n]=size(music); playsnd([bass;treble],Fs);
tune=[];
for k=1:m
f=music(k,1);
td=music(k,2);
tune=[tune, note_gen(f,td,Fs)];
end;
bass=tune;

2. Revise the program in (1) so that the musical piece is played in mono. Save the SCILAB program as
Sonatina_mono.sce.

SLU ECE 9
ECE 515 FL
Digital Signal Processing Laboratory Activity 4: Audio Synthesis Using SCILAB

//FREQUENCY TABLE (in Hz) //Sonatina treble part


do=261.63; dol=do/2; doh=2*do; music=[do q
re=293.67; rel=re/2; reh=2*re; re q
mi=329.63; mil=mi/2; mih=2*mi; mi q
fa=349.23; fal=fa/2; fah=2*fa; fa q
so=392; sol=so/2; soh=2*do; so q
la=440; lal=la/2; lah=2*la; la q
ti=493.9; til=ti/2; tih=2*ti; ti q
rest=0; doh q
ti q
//DURATION OF NOTES (in seconds) la q
w=2.00; ti q
h=1.00; so q
q=0.500; doh q
e=0.250; so q
s=0.125; mi q
so q
function x=note_gen(f, td, Fs) fa q
//function that generates the samples of a note la q
//f - frequency of the note so q
//td - duration of the note fa q
t=0:1/Fs:td-1/Fs mi q
x=sin(2*%pi*f*t); so q
endfunction fa q
mi q
//MAIN PROGRAM re h
stacksize(10^8); re h
Fs=8000; re h
rest h
//Load the text file containing the notes do q
//The notes should be contained in the variable re q
music mi q
//as a 2-column matrix. The first column contains fa q
//the notes and the second column contains the so q
duration la q
//of the notes ti q
doh q
[m,n]=size(music); ti q
la q
tune=[]; ti q
for k=1:m so q
f=music(k,1); doh q
td=music(k,2); so q
tune=[tune, note_gen(f,td,Fs)]; mi q
end; so q
fa q
//Sonatina bass la q
music=[dol w so q
mil w fa q
SLU ECE
10
ECE 515 FL
Digital Signal Processing Laboratory Activity 4: Audio Synthesis Using SCILAB

sol h mi q
fal h so q
mil w fa q
rel w mi q
dol w re q
sol q fa q
fal q mi q
sol q re q
fal q do h
sol q rest h];
fal q
mil q [m,n]=size(music);
rel q tune=[];
dol w for k=1:m
mil w f=music(k,1);
sol h td=music(k,2);
fal h tune=[tune, note_gen(f,td,Fs)];
mil w end;
rel w treble=tune;
dol w sonatina=0.5*(treble+bass);
sol h playsnd(sonatina,Fs);
sol h
dol h
rest h];

[m,n]=size(music);
tune=[];
for k=1:m
f=music(k,1);
td=music(k,2);
tune=[tune, note_gen(f,td,Fs)];
end;
bass=tune;

3. Write a Scilab program to generate the following periodic signals.


(a) Sawtooth periodic signal whose amplitude varies between -1 and 1 with a fundamental
frequency of 250Hz and duration of 5 seconds. Use Fs=44100Hz. Save the audio signal as
sawtooth.wav. Open the audio file with Goldwave to verify the waveform, amplitude and
frequency.

SLU ECE
11
ECE 515 FL
Digital Signal Processing Laboratory Activity 4: Audio Synthesis Using SCILAB

PROGRAM:
function [x]=sawgen(A, f, Fs, tdur)
T=1/f;
x=[];
for t=0:1/Fs:tdur
tcor = t- floor(t/T)*T; if tcor >= 0 & tcor < (T/2) then
x_temp = -A + (2*A/T)*tcor;
end;
if tcor >= (T/2) & tcor <T then
x_temp = -A + (2*A/T)*tcor;
end
x = [x, x_temp];
end;
endfunction;
x=sawgen(1,250,44100,5);
wavwrite(x,44100,'C:\Users\Desktop\sawtooth')

SLU ECE
12
ECE 515 FL
Digital Signal Processing Laboratory Activity 4: Audio Synthesis Using SCILAB

(b) Full-wave rectified sine wave with peak of 1, fundamental frequency of 250Hz, and
duration of 5 seconds. Use Fs=44100Hz. Save the audio file as FWsine.wav. Open the
audio file with Goldwave to verify the waveform, amplitude and frequency.
function [x]=FWsingen(A, f, Fs, tdur)
T=1/f;
x=[];
for t=0:1/Fs:tdur
tcor = t- floor(t/T)*T;
if tcor >= 0 & tcor < (T/2) then

(c) Square wave signal whose amplitude toggles between -1 and 1 with a fundamental
frequency of 250Hz and duration of 5 seconds. Use Fs=44100Hz. Save the audio file as
FWsine.wav. Open the audio file with Goldwave to verify the waveform, amplitude and
frequency.
Save the program as wavegen.sce.
PROGRAM:
function [x]=squaregen(A, f, Fs, tdur)
T=1/f;
x=[];
for t=0:1/Fs:tdur
tcor = t- floor(t/T)*T;
if tcor >= 0 & tcor < (T/2) then
x_temp = A;
end;
if tcor >= (T/2) & tcor <T then
x_temp = -A;
end
x = [x, x_temp];
end;
SLU ECE
13
ECE 515 FL
Digital Signal Processing Laboratory Activity 4: Audio Synthesis Using SCILAB

endfunction;
x=squaregen(1,250,44100,5);
wavwrite(x,44100,'C:\Users\Desktop\wavegen');

4. Write a Scilab program to generate the DTMF signal corresponding to the dialed numbers
077-423-2195. For DTMF signaling, for each number dialed the sum of two tones is generated
and transmitted. The frequencies of the two tones are determined using the DTMF frequency
matrix. If the number 6 is dialed, the frequencies of the two tones are 770Hz and 1477Hz.
1209 Hz 1336 Hz 1477 Hz 1663 Hz
-------------------------------------------------
697 Hz 1 2 3 A
770 Hz 4 5 6 B
852 Hz 7 8 9 C
941 Hz * 0 # D

Use Fs=8000Hz. Save the DTMF signal as DTMF.wav. Save the program as DTMF.sce.
PROGRAM:
fl1=697; fl2=770; fl3=852; fl4=941;
fh1=1209; fh2=1336; fh3=1477; fh4=1633;

Fs=8000;
t1=0:1/Fs:0.200;
t2=0:1/Fs:0.150;
break=zeros(t2);

one= (sin(2*%pi*fl1*t1)+sin(2*%pi*fh1*t1))/2;
two= (sin(2*%pi*fl1*t1)+sin(2*%pi*fh2*t1))/2;
three=(sin(2*%pi*fl1*t1)+sin(2*%pi*fh3*t1))/2;
four= (sin(2*%pi*fl2*t1)+sin(2*%pi*fh1*t1))/2;
five= (sin(2*%pi*fl2*t1)+sin(2*%pi*fh2*t1))/2;
SLU ECE
14
ECE 515 FL
Digital Signal Processing Laboratory Activity 4: Audio Synthesis Using SCILAB

six= (sin(2*%pi*fl2*t1)+sin(2*%pi*fh3*t1))/2;
seven=(sin(2*%pi*fl3*t1)+sin(2*%pi*fh1*t1))/2;
eigth=(sin(2*%pi*fl3*t1)+sin(2*%pi*fh2*t1))/2;
nine= (sin(2*%pi*fl3*t1)+sin(2*%pi*fh3*t1))/2;
zero= (sin(2*%pi*fl4*t1)+sin(2*%pi*fh1*t1))/2;

dialled_number=[break zero break seven break seven break four break two break three break two break one
break nine break five break];

// 1209 Hz 1336 Hz 1477 Hz 1663 Hz


//----------------------------------------------------------------------
//697 Hz 1 2 3 A
//770 Hz 4 5 6 B
//852 Hz 7 8 9 C
//941 Hz * 0 # D

playsnd(dialled_number,Fs);
wavwrite(dialled_number,Fs, D:\DSP Lab Files\DTMF.wav');
clf; plot2d(diallled_number);

V. CONCLUSION
In this activity, we have learned about the basic concepts of synthesis of audio and with these
basic concepts we are able to generate musical tunes, periodic signals and even DTMF signals using Scilab.
Using the concepts as basis we are able to create music through Scilab programming.
.

SLU ECE
15

You might also like