Musical Instrument Programs

Musical Instrument Programs

Updated 08/12/04

music.m

This program should analyze the data from any text source as long as the first column is time and the second is amplitude. It has been tested with data from the TI-82 and CBL system (99 points), the TI-83 and CBL system (512 points) and a ULI system (>2000 points). It asks for the name of a data file and then with user input, analyzes the data for the component frequencies. There is a minor bug in the wave reconstruction part of the program which remains to be fixed.

Examples of the output can be found at the Sound Project Home Page .



% This program loads a data file of time and pressure
% from any device.  It then plots the curve, analyzes for 
% fundamental frequency and overtones.

clear all


% Load data file

instr = input('Enter filename without .dat and with single quotes ');
load([instr '.dat']);
notes = eval(instr);
time = notes(:,1);
amp = notes(:,2);
q = size(time);

disp ('Position the cursor over a peak and click.');
disp ('Repeat for 3 successive peaks.');
pause

% This keeps the plots manageable.  Classroom testing showed that
% 500-600 data points still produced curves which could be viewed.
% This section trims the larger ULI files for viewing.

if q<600
	plot(time,amp);
else
	plot(time(1:500),amp(1:500));
end	 
title(instr);
xlabel('Time(s)');
ylabel('Pressure');

% Now select points to calculate frequency
s1=ginput(4);
t1=s1(1,1);
t2=s1(2,1);
t3=s1(3,1);
t4=s1(4,1);
freq(1,1)=1/(t2-t1);
freq(2,1)=1/(t3-t2);
freq(3,1)=1/(t4-t3);
avgfreq=mean(freq);
fprintf('Fundamental frequency = %4.2f \n',avgfreq);
pause

% Now perform fast fourier transform

disp('Now we perform Fourier transform of the data');
n = size(time);
% This section of code allows for the selection of the best power of
% 2 for the FFT calculation.  Unfortunately it takes the CBL-82 system to
% 64 data points - which produces poorer graphs than the original program

npower = fix (log2(n));
m = pow2(npower);
pause
Y=fft(amp,m);
Pyy=real(Y .* conj(Y) /m(1));

% The 0 point is not plotted, since it's amplitude may overwhelm other
% data.  So the f and Pyy functions are plotted from the second points on.

f=4000*(1:(m(1)/2 -1))/m(1);
figure
plot(f,Pyy(2:m(1)/2));
title(['Fast Fourier transform of ', instr]);

pause

% The user chooses frequencies from the FFT

x = input('How many frequencies to choose? ');
s2 = ginput(x);
t=0:max(time)/100:max(time);
color = 'mcyrgbmcyrgb';
figure
hold
for i = 1:x
	y(i,:)= s2(i,2) * cos(2 * pi * s2(i,1) * t);
	plot(t,y(i,:),color(i));
	pause
end

ytot=sum(y);
plot(t,ytot,'w');
pause
hold off

% The program plots some graphs for comparison

subplot(211),plot(t,ytot);
title('Reconstructed wave');
if q<600
	subplot(212),plot(time,amp);
else
	subplot(212),plot(time(1:500),amp(1:500));
end	 

title('Original wave');
subplot(111);

s3(1,:)=reshape(s2(:,1),1,x);
s3(2,:)=reshape(s2(:,2),1,x);

disp('Frequencies      Amplitudes');
fprintf('  %4.1f          %2.4f\n',s3);

pause
figure

if q<600
	subplot(211),plot(time,amp);
else
	subplot(211),plot(time(1:500),amp(1:500));
end	 

title('Original wave');
subplot(212), plot(f,Pyy(2:m/2));
title('Frequency Spectrum');
subplot(111);




musicCBL.m 11/13/96

This program was written to analyze the output from a CBL and the TI-82 calculator (99 data points). It asks for the name of a data file and then with user input, analyzes the data for the component frequencies. Examples of the output can be found at the Sound Project Home Page .
% This program loads a data file of time and pressure
% from CBL -microphone.  It then plots the curve, analyzes for 
% fundamental frequncy and overtones.

clear all


% Load data file

instr = input('Enter filename without .dat and with single quotes ');
load([instr '.dat']);
notes = eval(instr);
time = notes(:,1);
amp = notes(:,2);

disp ('Position the cursor over a peak and click.');
disp ('Repeat for 3 successive peaks.');
pause
plot(time,amp);
title(instr);
xlabel('Time(s)');
ylabel('Pressure');

% Now select points to calculate frequency
s1=ginput(4);
t1=s1(1,1);
t2=s1(2,1);
t3=s1(3,1);
t4=s1(4,1);
freq(1,1)=1/(t2-t1);
freq(2,1)=1/(t3-t2);
freq(3,1)=1/(t4-t3);
avgfreq=mean(freq);
fprintf('Fundamental frequency = %4.2f \n',avgfreq);
pause
figure

% Now perform fast fourier transform
disp('Now we perform Fourier transform of the data');
pause
Y=fft(amp,128);
Pyy=real(Y .* conj(Y) /128);
f=4000*(0:63)/128;
plot(f,Pyy(1:64));
title(['Fast Fourier transform of ', instr]);

pause
%y=[];
x = input('How many frequencies to choose?');
s2 = ginput(x);
t=0:0.00025:0.025;
color = 'mcyrgbmcyrgb';
figure
hold
for i = 1:x
	y(i,:)= s2(i,2) * cos(2 * pi * s2(i,1) * t);
	plot(t,y(i,:),color(i));
	pause
end

ytot=sum(y);
plot(t,ytot,'w');
pause
hold off
subplot(211),plot(t,ytot);
title('Reconstructed wave');
subplot(212), plot(time, amp);
title('Original wave');
subplot(111);

s3(1,:)=reshape(s2(:,1),1,x);
s3(2,:)=reshape(s2(:,2),1,x);

disp('Frequencies      Amplitudes');
fprintf('  %4.1f          %2.3f\n',s3);