You are on page 1of 12

A Spectral Analysis of Music

Jerey Uslan University of Washington February 6, 2012

Abstract An implementation of spectral methods to analyze various music pieces. Filters will be applied to produce spectrograms of the music as a representation of the notes.

Introduction and Overview

The task is to nd the most appropriate window for the most appropriate lter. A general survey of options should yield a preferred method. Spectrograms will be produced to visualize the clarity of the data. For Analysis of Mary of Had a Little Lamb a Gabor lter will be used to isolate the frequency of each note.

Theoretical Background

In the rst part of the project, Handels Messiah will be analyzed using Gabor windows. Windows used will include a Gaussian, the mexican hat wavelet, and a Shannon step-function. Spectrogram will be produced from the processed data. In the second part a simple Gabor window will be used to identify the musical notes of two recordings of Mary Had a Little Lamb, the rst recording is of a piano, the second is of a recorder.

3
3.1

Algorithm Implementation and Development


Gabor Spectrograms

To begin my algorithm I established a basis space for normal coordinates, fourier coordinates (rescaled for Hertz by dividing by 2pi), and shifted fourier coordinates (for plotting purposes). I then plotted the data in time and frequency to better visually understand the data I was working with (g.1):

Figure 1: Handels Messiah I will limit the exploration at 2500 Hz as that is where the majority of activity occurs. Then within a for loop I build a Gaussian lter and apply it to the data in the fourier domain. I collect the slices by sliding the lter along the time spectrum and save the results to a matrix for the spectrogram. The gures below show the resulting spectrograms at dierent widths:

Figure 2: Gabor lter with width 15

Figure 3: Gabor lter with width 20

Figure 4: Gabor lter with width 25 I then repeated the process using the Mexican hat wavelet:

Figure 5: Mexican hat lter with width .1

Figure 6: Mexican hat lter with width 0.05

Figure 7: Mexican hat lter with width .005 I then repeated the process using the Shannon step-function:

Figure 8: Shannon step-function lter with width 10

Figure 9: Shannon step-function lter with width 15

Figure 10: Shannon step-function lter with width 20

3.2

Mary Had a Little Lamb

To begin my algorithm I established a basis space for normal coordinates, fourier coordinates (rescaled for Hertz by dividing by 2pi), and shifted fourier coordinates (for plotting purposes). I then plotted the data in time and frequency to better visually understand the data I was working with (g.11):

Figure 11: Mary Had a Little Lamb on the piano I then constructed a vector of time locations of each note and applied a Gaussian lter to each point to nd the frequency at each time. I did this by applying the lter to the transformed music vector and saving the results by slices in a vector. I then plugged the index of the maximum value of each time slice into the frequency coordinates to nd the frequency of the note. Below is the spectrogram of the song played on the piano with a Gaussian lter of width 5:

Figure 12: Mary Had a Little Lamb piano spectrum I then repeated the process for the music played on the recorder:

Figure 13: Mary Had a Little Lamb on the recorder Below is the spectrogram of the song played on the recorder with a Gaussian lter of width 5:

Figure 14: Mary Had a Little Lamb recorder spectrum

Computational Results
For analysis of Handels Messiah I found that the Gaussian lter with a window width of 20, a mexican hat lter of width .05, and a shannon step-function of width 15 were best to produce a spectrogram. For analysis of Mary Had a Little Lamb I used a Gaussian window with a width of 30. For the Mary Had a Little Lamb played on the piano I found frequencies of: 1.0e+002 * 3.195 2.865 2.557 2.863 3.190 3.182 3.201 2.865 2.866 2.862 3.196 3.186 3.197 3.200 2.864 2.558 2.865 3.202 3.199 3.196 3.203 2.868 2.875 3.198 2.871 2.560 8

For the Mary Had a Little Lamb played on the piano I found frequencies of: 1.0e+003 * 1.029 0.916 0.817 0.923 1.033 1.038 1.029 0.920 0.906 0.913 1.030 1.035 1.034 1.034 0.911 0.819 0.909 1.039 1.031 1.026 1.026 0.910 0.913 1.025 0.910 0.817 While the notes for each music piece were the same, they were played at signicantly dierent frequencies.

Summary and Conclusions

Using the frequency results obtained for Mary Had a Little lamb I can reproduce the music score for the piano as: edcdeeedddeeeedcdeeeeddedc Using the frequency results obtained for Mary Had a Little lamb I can reproduce the music score for the recorder as: c ab a ab c c c ab ab ab c c c c ab a ab c c c c ab ab c ab a We can see some the spectrograms of the music (gures 12 and 14) that the dierence between the instruments (besides the keys they are played in) is the overtones. The piano plays overtones at 2 and 3 times the frequency of the note while the recorder has no apparent overtone.

Appendix A MATLAB functions


I used the t command to convert the music data to the fourier domain. I use the tshift command to move the zero-frequency component to the center of the array for plotting purposes. I uses the nd command to locate the maximum value of the averaged data. I used the pcolor command to create a spectrogram of the frequency data. I used the wavread command to import .wav music les. I used the audioplayer and playblocking command to play imported music/

Appendix B MATLAB codes

clear all; close all; clc; load handel v = y/2; L=10; n=length(v); t2=linspace(0,L,n+1); t=t2(1:n); k=(1/L)*[0:ceil(n/2-1) ceil(-n/2):-1]; ks=fftshift(k); p8 = audioplayer(v,Fs); playblocking(p8); vt=fft(v); figure(1) 9

subplot(2,1,1) % Time domain plot(t,v,k) set(gca,Fontsize,[14]), xlabel(Time (t)), ylabel(Amplitude) title(Signal of Interest, v(n)); subplot(2,1,2) % Fourier domain plot(ks,abs(fftshift(vt))/max(abs(vt)),k); axis([0 2500 0 1]) set(gca,Fontsize,[14]) xlabel(frequency (Hz)), ylabel(FFT(v)) vgt_spec=[]; tslide=0:0.1:10; w=.5; figure(2) for j=1:length(tslide) % g=1/sqrt(w)*(sinc(w*(t-tslide(j))).*exp(2i*pi*(t-tslide(j)))); g=exp(-w*(t-tslide(j)).^2); % Gabor % g=((1/sqrt(w))*(1-((t-tslide(j))/w).^2)).*exp((-((t-tslide(j))/w).^2)/2); vg=g.*v; vgt=fft(vg); vgt_spec=[vgt_spec; abs(fftshift(vgt))]; subplot(3,1,1), plot(t,v,k,t,g,r) subplot(3,1,2), plot(t,vg,k) subplot(3,1,3), plot(ks,abs(fftshift(vgt))/max(abs(vgt))) drawnow pause(0.01) end figure(3) pcolor(tslide,ks,vgt_spec.), shading interp set(gca,Ylim,[0 2500],Fontsize,[14]) colormap(hot) clear all; close all; clc; format long tr_piano=15; % record time in seconds y=wavread(music1); y=y; w=5; p8 = audioplayer(y,Fs); playblocking(p8); L=16; n=length(y); t2=linspace(0,L,n+1); t=t2(1:n); k=(1/L)*[0:ceil(n/2-1) ceil(-n/2):-1]; ks=fftshift(k); yt=fft(y); figure(1) subplot(2,1,1) % Time domain plot(t,y,k) 10

axis([0 15 0 1]) set(gca,Fontsize,[14]), xlabel(Time (t)), ylabel(Amplitude) title(Mary had a little lamb (piano), y(n)); subplot(2,1,2) % Fourier domain plot(ks,abs(fftshift(yt))/max(abs(yt)),k); axis([0 400 0 1]) set(gca,Fontsize,[14]) xlabel(frequency (Hz)), ylabel(FFT(y)) tslide=[.8,1.3,1.8,2.3,2.7,3.2,3.8,4.6,5.1,5.6,6.4,7,... 7.4,8.3,8.6,9.2,9.6,10.1,10.6,11,11.6,12,12.42,... 12.9,13.3 14]; ygt_spec=[]; figure(2) for j=1:length(tslide) g=exp(-w*(t-tslide(j)).^2); % Gabor yg=g.*y; ygt=fft(yg); ygt_spec=[ygt_spec; abs(fftshift(ygt))]; subplot(3,1,1), plot(t,y,k,t,g,r) axis([0 14 0 1]) subplot(3,1,2), plot(t,yg,k) axis([0 14 0 .3]) subplot(3,1,3), plot(ks,abs(fftshift(ygt))/max(abs(ygt))) axis([0 1000 0 1]) drawnow pause(0.01) end figure(3) pcolor(tslide,ks,vgt_spec.), shading interp set(gca,Ylim,[0 1500],Fontsize,[14]) colormap(hot) freqs1=zeros(26,2); for j=1:26 notes=find(ygt_spec(j,:) == max(ygt_spec(j,:))); freqs1(j,1)=ks(notes(1)); freqs1(j,2)=ks(notes(2)); end freqs1 tr_rec=14; % record time in seconds y=wavread(music2); Fs=length(y)/tr_rec; y=y; w=40; L=14; n=length(y); t2=linspace(0,L,n+1); t=t2(1:n); k=(1/L)*[0:ceil(n/2-1) ceil(-n/2):-1]; ks=fftshift(k); % p8 = audioplayer(y,Fs); playblocking(p8); 11

yt=fft(y); figure(4) subplot(2,1,1) % Time domain plot(t,y,k) axis([0 14 0 1]) set(gca,Fontsize,[14]), xlabel(Time (t)), ylabel(Amplitude) title(Mary had a little lamb (piano), y(n)); subplot(2,1,2) % Fourier domain plot(ks,abs(fftshift(yt))/max(abs(yt)),k); axis([0 1200 0 1]) set(gca,Fontsize,[14]) xlabel(frequency (Hz)), ylabel(FFT(y)) figure(5) tslide=[.18,.58,1,1.5,2.1,2.5,3,3.7,4.2,4.7,5.6,6,... 6.4,7.4,7.9,8.2,8.8,9.2,9.6,10.1,10.5,11.1,11.4,12,12.4,13.3]; ygt_spec=[]; for j=1:length(tslide) g=exp(-w*(t-tslide(j)).^2); % Gabor yg=g.*y; ygt=fft(yg); ygt_spec=[ygt_spec; abs(fftshift(ygt))]; subplot(3,1,1), plot(t,y,k,t,g,r) axis([0 14 0 1]) subplot(3,1,2), plot(t,yg,k) axis([0 14 0 .3]) subplot(3,1,3), plot(ks,abs(fftshift(ygt))/max(abs(ygt))) axis([0 1200 0 1]) drawnow % pause(0.01) end figure(6) pcolor(tslide,ks,vgt_spec.), shading interp set(gca,Ylim,[0 1500],Fontsize,[14]) colormap(hot) freqs2=zeros(26,2); for j=1:26 notes=find(ygt_spec(j,:) == max(ygt_spec(j,:))); freqs2(j,1)=ks(notes(1)); freqs2(j,2)=ks(notes(2)); end freqs2

12

You might also like