You are on page 1of 21

1

1. Introduction
In this project, it was aimed to separate the audio of the octaves produced by a piano from
each other. This aim was tried to be reached in two ways. The first way was to make use of
MATLAB Simulink. The second way was to make use of TMS 320C6713 DSK Digital Signal
Processor product of Texas Instruments.
To make use of TMS 320C6713 DSK Digital Signal Processor Product of Texas
Instruments, Code Composer Studio which is software designed for embedding the C code
written for the DSP card was used. Convolution procedure was designed as a function in the C
code and input audio signal was put in convolution processes with the digital FIR filters
according to the frequency range of each piano octave. FIR filter coefficients are to be obtained
using MATLAB Simulink. These coefficients were used in the C code for the DSP.
2. Analysis of the Notes of the Sound from a Piano
The sounds from musical instruments are time-evolving super-positions of several
sinusoidal waves. In the frequency spectrum of the note E4 in the sound from a piano, there are
peaks that are approximately located at 330 Hz, 660 Hz, 990 Hz, 1320 Hz, and 1620 Hz. These
frequencies are all scalar multiples of the smallest frequency which is 330 Hz and is called the
fundamental frequency for the note E4. The integer multiples of the fundamental frequency are
called the overtones.

Figure 2.1.The time response of the piano note E4 in (a), the frequency response of the piano
note E4 in (b)
As seen in figure 2.1, FFT of the piano note E4 has the smallest component at 330 Hz
which is the fundamental frequency for it. The overtones for this note are also seen in the
frequency spectrum.
2

. There is a certain rule to calculate the fundamental frequency of each note. Every piano is
designed according to this calculation rule and their fundamental frequencies are determined in
this way. The rule is that the fundamental frequency of the first note, which is produced by the
first button of the piano, is 27.5 Hz. The second note’s fundamental frequency is higher by a
factor of twelfth root of two than the first note and the third note’s fundamental frequency is
higher by a factor of twelfth root of two than the second note and so on. There are 88 buttons on a
piano. The fundamental frequency of each note is calculated using MATLAB to demonstrate the
frequency spectrum of piano audio. The .m file written using MATLAB for this calculation is
added to appendix part of the report.

Figure 2.2.Fundamental Frequency versus the Piano Key Graph


3

Figure 2.3.Corresponding Fundamental Frequency for Each Key on a Piano


4

When the figure 2.2 is examined carefully, it is clearly observed that a note of next octave
is the double of the note of previous octave. For instance, C1’s fundamental frequency is 32.7 Hz
and C2’s fundamental frequency is 65.4 Hz. For the fundamental frequency of every note in the
frequency spectrum, this is valid.
In the design of the filter, it should be used a band-pass FIR filter for the octave that is to
be filtered. The filter for each octave is to be implemented using MATLAB Simulink.
The audio frequency spectrum of the total of the notes produced by the piano is the
superposition of cosines or sins with the fundamental frequencies for all notes. There is no other
frequency component in the audio frequency spectrum.
3. Implementation of the Filters
First of all, frequency band of each octave was determined according to the fundamental
frequencies of the notes in the octave. This information was shown in the following table which
is table 3.1.
Table 3.1.The Frequency Band of each Octave in the Sound of a Piano
Octave The Fundamental Frequency of the First The Fundamental Frequency of the Last
Number Note in the Octave Note in the Octave
1 32.703Hz 61.735Hz
2 65.406Hz 123.47Hz
3 130.81Hz 246.94Hz
4 261.63Hz 493.88Hz
5 523.25Hz 987.77Hz
6 1046.5Hz 1979.5Hz
7 2093Hz 3951.1Hz
According to the information of frequency band of each octave, cut-off frequencies of
each filter were determined. The fundamental frequency of the first note in the octave is the lower
cut-off frequency of the band-pass filter of the corresponding octave and the fundamental
frequency of the last note in the octave is the upper cut-off frequency of the band-pass filter of
the corresponding octave.
3.1. Implementation Using MATLAB Simulink
3.1.1. Determination of Filter Type
To implement the desired band-pass filters on MATLAB Simulink, the type of the filter is
determined firstly. The FIR filter was chosen in the implementation, because its implementation
is easier than the IIR filter and there is no possibility for the filter to be unstable whereas IIR
5

filters can be unstable for some situations since they include denominator part in its transfer
function. FIR filters include only nominator in its transfer function preventing the filter to
become unstable. Then, the type of the FIR filter was tried to be determined. Window filter was
chosen in the implementation of the band-pass filter whose frequency band is very small.
Equiripple FIR filter was used in the implementation of the band-pass filter whose frequency
band is relatively large.
To determine the type of FIR filter, window FIR filter and equiripple FIR filter were
compared firstly. In the frequency response of equiripple FIR filter, side lobes of the response are
in the same height. In the response of window FIR filter, the height of side lobes is decreasing
after the cut-off frequencies. As the frequency band of the band-pass filter decreases, the side
lobes are getting closer to the passing part of the filter. For the equiripple FIR filter, there is very
little difference between the side lobes and passing part of the filter. Thus, equiripple FIR filter is
not good for the band-pass filters whose frequency band is very small.

Figure 3.1.1.1.The Frequency Response of the Equiripple FIR Filter with Relatively Large
Frequency Band

Figure 3.1.1.2.The Frequency Response of the Window FIR Filter with Relatively Large
Frequency Band
6

Figure 3.1.1.3.The Frequency Response of the Equiripple FIR Filter with Relatively Small
Frequency Band

Figure 3.1.1.4.The Frequency Response of the Window FIR Filter with Relatively Small
Frequency Band
In figure 3.1.1.1 and in figure 3.1.1.2, the comparison of the frequency response between
equiripple band-pass FIR filter and window band-pass FIR filter is seen when bandwidth of the
band-pass filter is relatively large. In figure 3.1.1.3 and in figure 3.1.1.4, the comparison of the
frequency response between equiripple band-pass FIR filter and window band-pass FIR filter is
seen when bandwidth of the band-pass filter is relatively small. As a result, in the band-pass filter
design, window band-pass FIR filter is to be used for small bandwidth and equiripple band-pass
FIR filter is to be used for large bandwidth.
As seen in figure 3.1.1.1 and figure 3.1.1.2, there is not a big difference between window
band-pass FIR filter and equiripple band-pass FIR filter for large bandwidth. Thus, window band-
pass FIR filters are used for all filters in the project.
3.1.2. Design of Filters
Depending on the choice of filter type, band-pass filters are designed in MATLAB
Simulink for all octaves according to their frequency band.
Since the filter type is chosen as the window band-pass FIR filter, corresponding
parameters for this type of filter is to be determined. The fundamental frequency of the first note
7

in the octave is the lower cut-off frequency for the band-pass filter and the fundamental
frequency of the last note in the octave is the upper cut-off frequency for the band-pass filter.
Kaiser window was chosen as the window type. Attenuation in the stop band can be
determined according to β parameter of the filter. As β increases, stop band attenuation increases.
Thus β should be chosen as large as possible. However, as β increases, the desired bandwidth
increases to compensate the large attenuation in the stop band. Thus, for a band-pass filter with
small bandwidth, β is chosen small and for a band-pass filter with large bandwidth, β is chosen
relatively high. Depending on this criteria, β increases as octave number increases since the
bandwidth of the filter increases as octave number increases due to the exponentially increasing
relationship of the fundamental frequency of each note as seen in table 3.1.2.1.
Table 3.1.2.1.The Bandwidth of the Band-pass Filters for each Octave
Octave The Bandwidth of the β
Number Filter
1 29.032Hz 0.1
2 58.064Hz 0.2
3 116.13Hz 0.4
4 232.25Hz 0.8
5 464.52Hz 1.6
6 933Hz 3.2
7 1858.1Hz 6.4
3.1.2.1. The Band-pass Filters Implemented in MATLAB Simulink
The band-pass filters designed on MATLAB Simulink are one-by-one shown on the
following figures. Cut-off frequencies and β parameter are the only changing parameters. These
parameters for all octaves are given in table 3.1.2.1.1.
Table 3.1.2.1.1.The Parameters of the Window FIR Band-pass Filters for all Octaves
Lower Cut-off β
Octave Number Frequency Upper Cut-off Frequency
1 32.703Hz 61.735Hz 0.1
2 65.406Hz 123.47Hz 0.2
3 130.81Hz 246.94Hz 0.4
4 261.63Hz 493.88Hz 0.8
5 523.25Hz 987.77Hz 1.6
6 1046.5Hz 1979.5Hz 3.2
7 2093Hz 3951.1Hz 6.4
8

Figure 3.1.2.1.1.The Parameters of the Band-pass Filter for the 1st Octave

Figure 3.1.2.1.2.The Parameters of the Band-pass Filter for the 2nd Octave
9

Figure 3.1.2.1.3.The Parameters of the Band-pass Filter for the 3rd Octave

Figure 3.1.2.1.4.The Parameters of the Band-pass Filter for the 4th Octave
10

Figure 3.1.2.1.5.The Parameters of the Band-pass Filter for the 5th Octave

Figure 3.1.2.1.6.The Parameters of the Band-pass Filter for the 6th Octave
11

Figure 3.1.2.1.7.The Parameters of the Band-pass Filter for the 7th Octave
3.1.2.2. Final Simulink Model of the Design
After designing the band-pass filters in MATLAB Simulink, they are connected in parallel
and after each of the band-pass filter a slider gain is placed in series. To determine the octave of a
note, slider gain blocks are used. Only one of the slider gain block is set to full load and the other
ones are set to zero. One by one every slider gain is set to full load. When the sound of the note is
heard for one of the slider gain block which is set to full load, the band-pass filter put before the
slider gain block determines the octave of the note.
The final set-up of the model is shown in figure 3.1.2.2.1. This model can be used in a
place with a piano since it receives audio from the environment. Using the procedure mentioned
in the previous paragraph, the octave in which the note played by the piano exists is determined.
12

Figure 3.1.2.2.1.The Final Set-up of the Simulink Model


3.2. Implementation of the Design on TMS 320C6713 DSK
To implement the design on TMS 320C6713 DSK, a C code is necessary. Also, filter
coefficients of the band-pass filters designed are needed to be used in the code. The filter
coefficients are obtained from the filters designed in MATLAB Simulink.
In the C code of the design, convolution procedure is applied to the input signal with the
filters. To determine which of the filter is used, DIP switches are used with an “if statement” and
convolution procedure is applied after the “if statement”. Written C code is added to the appendix
part of the report.
13

4. CONCLUSION
Before starting the project, it was aimed to separate the octaves of the sound from a piano.
In other words, the octave of a note played by a piano was tried to be determined. This aim was
almost reached because the sound of a note from an octave was separated using the
corresponding digital FIR filter. However, it was not separated completely because of the
overtones of the notes. For instance, a note whose fundamental frequency is 440 Hz has an
overtone at 880 Hz and the sound of this note can pass through the band-pass FIR filter of 4th
octave and 5th octave due to the overtone. Despite this, the sound heard with the band-pass filter
of 4th octave is more powerful than the sound heard with the band-pass filter of 5th octave. As a
result, due to the overtones, it is not possible to separate them completely for neighbor octaves.
The use of DIP switches in this project helped a lot because it made possible to execute
the desired work in only one C source code. If they were not used, it was needed to use separate
codes for all octaves.

5. REFERENCES
14

1. http://www.phys.unsw.edu.au/jw/sound.spectrum.html
2. http://www.sengpielaudio.com/calculator-notenames.htm
3. https://ccrma.stanford.edu/~jos/sasp/Window_Method.html
4. https://ccrma.stanford.edu/courses/422/projects/kbd/
5. http://www.scribd.com/doc/35298446/D-S-P-MATLAB-Theory-Experiments
6. http://www.scribd.com/doc/38762942/Ccs-Manual-1

APPENDIX-A MATLAB Code for the Calculation of Fundamental Frequency


x=zeros(1,88); %a vector, full of zeros, in which fundamental frequency of each note is placed
15

x(1)=27.500 %the starter value for fundamental frequency


for i=1:1:87 %the loop to calculate the fundamental frequency of each note
x(i+1)=(2^(1/12))*x(i) %multiplication by the factor to determine the following note's
fundamental frequency
end
n=1:1:88;
stem(n,x(n)); %stem plot for the fundamental frequency of each note

APPENDIX-B C Source Code of the Design


#include "dsk6713.h"
16

#include "dsk6713_dip.h"
#include "dsk6713_aic23.h"
#include "stdlib.h"
#include "math.h"

/* Configure Codec */
DSK6713_AIC23_Config config = {
0x0017, /* 0 - DSK6713_AIC23_LEFTINVOL Left line input channel volume */
0x0017, /* 1 - DSK6713_AIC23_RIGHTINVOL Right line input channel volume */
0x01f9, /* 2 - DSK6713_AIC23_LEFTHPVOL Left channel headphone volume */
0x01f9, /* 3 - DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume */
0x0014, /* 4 - DSK6713_AIC23_ANAPATH Analog audio path control */
0x0000, /* 5 - DSK6713_AIC23_DIGPATH Digital audio path control */
0x0000, /* 6 - DSK6713_AIC23_POWERDOWN Power down control */
0x0043, /* 7 - DSK6713_AIC23_DIGIF Digital audio interface format */
0x008d, /* 8 - DSK6713_AIC23_SAMPLERATE Sample rate control */
0x0001 /* 9 - DSK6713_AIC23_DIGACT Digital interface activation */ }

void main(){

DSK6713_AIC23_CodecHandle hCodec;
float OUT_L,OUT_R,IN_L;

DSK6713_init(); /* Initialize the board support library. It must be called first */


DSK6713_DIP_init(); /* Initialize DIP Switches */
hCodec = DSK6713_AIC23_openCodec(0, &config); /* Start the codec */

DSK6713_AIC23_setFreq(hCodec, DSK6713_AIC23_FREQ_8KHZ ); /* Change sampling


rate to to 8KHz */
17

float x[128],y[128];
int i,j;

while (1) {
while (!DSK6713_AIC23_read(hCodec, &IN_L)); /* Read sample from the left channel */

x[0]=IN_L;

/*Shifting the audio input in an array*/

for(i=127;i>0;i--){
x[i]=x[i-1];
}

if(DSK6713_DIP_get(1)==1 && DSK6713_DIP_get(2)==0 && DSK6713_DIP_get(3)==0 &&


DSK6713_DIP_get(4)==0){

/*Convolution Procedure with the band-pass filter for the 1st Octave */

for(i=127;i>=0;i--){
y[i]=0;
for(j=0;j<=i;j++){
y[i]=y[i]+x[j]*oct1[i-j];
}
}
OUT_L=y[0];
OUT_R=y[0];
}

if(DSK6713_DIP_get(1)==0 && DSK6713_DIP_get(2)==1 && DSK6713_DIP_get(3)==0 &&


DSK6713_DIP_get(4)==0){
18

/*Convolution Procedure with the band-pass filter for the 2nd Octave */

for(i=127;i>=0;i--){
y[i]=0;
for(j=0;j<=i;j++){
y[i]=y[i]+x[j]*oct2[i-j];
}
}
OUT_L=y[0];
OUT_R=y[0];
}

if(DSK6713_DIP_get(1)==1 && DSK6713_DIP_get(2)==1 && DSK6713_DIP_get(3)==0 &&


DSK6713_DIP_get(4)==0){

/*Convolution Procedure with the band-pass filter for the 3rd Octave */

for(i=127;i>=0;i--){
y[i]=0;
for(j=0;j<=i;j++){
y[i]=y[i]+x[j]*oct3[i-j];
}
}
OUT_L=y[0];
OUT_R=y[0];
}

if(DSK6713_DIP_get(1)==0 && DSK6713_DIP_get(2)==0 && DSK6713_DIP_get(3)==1 &&


DSK6713_DIP_get(4)==0){
19

/*Convolution Procedure with the band-pass filter for the 4th Octave */

for(i=127;i>=0;i--){
y[i]=0;
for(j=0;j<=i;j++){
y[i]=y[i]+x[j]*oct4[i-j];
}
}
OUT_L=y[0];
OUT_R=y[0];
}

if(DSK6713_DIP_get(1)==1 && DSK6713_DIP_get(2)==0 && DSK6713_DIP_get(3)==1 &&


DSK6713_DIP_get(4)==0){

/*Convolution Procedure with the band-pass filter for the 5th Octave */

for(i=127;i>=0;i--){
y[i]=0;
for(j=0;j<=i;j++){
y[i]=y[i]+x[j]*oct5[i-j];
}
}
OUT_L=y[0];
OUT_R=y[0];
}

if(DSK6713_DIP_get(1)==0 && DSK6713_DIP_get(2)==1 && DSK6713_DIP_get(3)==1 &&


DSK6713_DIP_get(4)==0){

/*Convolution Procedure with the band-pass filter for the 6th Octave */
20

for(i=127;i>=0;i--){
y[i]=0;
for(j=0;j<=i;j++){
y[i]=y[i]+x[j]*oct6[i-j];
}
}
OUT_L=y[0];
OUT_R=y[0];
}

if(DSK6713_DIP_get(1)==1 && DSK6713_DIP_get(2)==1 && DSK6713_DIP_get(3)==1 &&


DSK6713_DIP_get(4)==0){

/*Convolution Procedure with the band-pass filter for the 7th Octave */

for(i=127;i>=0;i--){
y[i]=0;
for(j=0;j<=i;j++){
y[i]=y[i]+x[j]*oct7[i-j];
}
}
OUT_L=y[0];
OUT_R=y[0];
}

while (!DSK6713_AIC23_write(hCodec, OUT_L)); /* Send the sample to the left channel


*/
while (!DSK6713_AIC23_write(hCodec, OUT_R)); /* Send the sample to the right
channel */
}
21

DSK6713_AIC23_closeCodec(hCodec); /* Close the codec */


}

You might also like