Hi everyone,
I am, almost, done in my note dedector project. When I was starting this project, I was so hopeless but now, I had gone so much progress.
I can dedect these notes C4,D4,E4,F4,G4,A4,B4,C5 but, C4 and C5 have a problem. They gives same harmonic peaks. You can copy the codes which are at below and also you are able to compile your matlab. if you have a microphone and musical enstruments (if you have a smart phone, you can install perfect piano I suggest to use it)
You can see these peaks. In fact, C5 must give the first peak around 1000-1100hz but it gives first peak around 520-530hz.
So, my program can not clearly realize between C4 and C5. How can I solve this problem? why does first peak of C5 start from 520-530hz?
clc
clear
a = 0; b = 0;
Do1 = 0; Re1 = 0; Mi1 = 0; Fa1 = 0; Sol1 = 0; La1 = 0; Si1 = 0; DoHigh1 = 0;
Do2 = 0; Re2 = 0; Mi2 = 0; Fa2 = 0; Sol2 = 0; La2 = 0; Si2 = 0; DoHigh2 = 0;
Do3 = 0; Re3 = 0; Mi3 = 0; Fa3 = 0; Sol3 = 0; La3 = 0; Si3 = 0; DoHigh3 = 0;
while(1)
Fs=8000; %Sampling frequency
T=1; %record seconds
recObj = audiorecorder(Fs,8,1);
recordblocking(recObj, 1);
s = getaudiodata(recObj); % Store Data in Double-Precision Array
for u=1:2000 %Remove Intital Samples of Sound Because of Initial Conditions
s(u)=0;
end
F=abs(fft(s,Fs)); % Absolute Fourier Transform of Sound
F=F(1:Fs/2,1); % Half of FFT
lf=150; % Lowest Frequencies
for i=1:lf % Remove Low Frequencies Because of Noise and Microphone Limits
F(i)=0;
end
for i=lf:Fs/2 % Remove Frequencies Witch are Small, Because of Noise
if F(i)<4
F(i)=0;
end;
end
[a,b]= max(abs(F));
% fiding peak values of sound and writing
[pks, locs] = findpeaks(abs(F))
[m,n] = size(pks);
if m>0
for i=1:m
% for C
if locs(i,1) >= 520 && locs(i,1) <= 530
Do1 = 1;
end
if locs(i,1) >= 1045 && locs(i,1) <= 1055
Do2 = 1;
end
if locs(i,1) >= 1840 && locs(i,1) <= 1860
Do3 = 1;
end
% for D
if locs(i,1) >= 580 && locs(i,1) <= 595
Re1 = 1;
end
if locs(i,1) >= 1170 && locs(i,1) <= 1190
Re2 = 1;
end
if locs(i,1) >= 1470 && locs(i,1) <= 1485
Re3 = 1;
end
% for E
if locs(i,1) >= 660 && locs(i,1) <= 675
Mi1 = 1;
end
if locs(i,1) >= 985 && locs(i,1) <= 998
Mi2 = 1; Mi3 = 1;
end
% for F
if locs(i,1) >= 680 && locs(i,1) <= 700
Fa1 = 1;
end
if locs(i,1) >= 1045 && locs(i,1) <= 1059
Fa2 = 1;
end
if locs(i,1) >= 1745 && locs(i,1) <= 1760
Fa3 = 1;
end
% for G
if locs(i,1) >= 785 && locs(i,1) <= 795
Sol1 = 1;
end
if locs(i,1) >= 1175 && locs(i,1) <= 1195
Sol2 = 1;
end
if locs(i,1) >= 1965 && locs(i,1) <= 1985
Sol3 = 1;
end
% for A
if locs(i,1) >= 435 && locs(i,1) <= 445
La1 = 1;
end
if locs(i,1) >= 870 && locs(i,1) <= 890
La2 = 1;
end
if locs(i,1) >= 1750 && locs(i,1) <= 1780
La3 = 1;
end
% for B
if locs(i,1) >= 490 && locs(i,1) <= 500
Si1 = 1;
end
if locs(i,1) >= 980 && locs(i,1) <= 1000
Si2 = 1;
end
if locs(i,1) >= 1475 && locs(i,1) <= 1498
Si3 = 1;
end
% for HighC
if locs(i,1) >= 515 && locs(i,1) <= 530
DoHigh1 = 1;
end
if locs(i,1) >= 1035 && locs(i,1) <= 1055
DoHigh2 = 1;
end
if locs(i,1) >= 2090 && locs(i,1) <= 2120
DoHigh3 = 1; Do1=0; Do2=0; Do3=0;
end
end
end
% Plot the waveform.
subplot(2,1,1);
plot(s);
subplot(2,1,2);
plot(abs(F),'r');
if m>0
if Do1 == 1 && Do2 == 1 && Do3 == 1
fprintf('Note is "C", ');
end
if Re1 == 1 && Re2 == 1 && Re3 == 1
fprintf('Note is "D", ');
end
if Mi1 == 1 && Mi2 == 1 && Mi3 == 1
fprintf('Note is "E", ');
end
if Fa1 == 1 && Fa2 == 1 && Fa3 == 1
fprintf('Note is "F", ');
end
if Sol1 == 1 && Sol2 == 1 && Sol3 == 1
fprintf('Note is "G", ');
end
if La1 == 1 && La2 == 1 && La3 == 1
fprintf('Note is "A", ');
end
if Si1 == 1 && Si2 == 1 && Si3 == 1
fprintf('Note is "B", ');
end
if DoHigh1 == 1 && DoHigh2 == 1 && DoHigh3 == 1
fprintf('Note is high "C", ');
end
fprintf('\n');
end
Do1 = 0; Re1 = 0; Mi1 = 0; Fa1 = 0; Sol1 = 0; La1 = 0; Si1 = 0; DoHigh1 = 0;
Do2 = 0; Re2 = 0; Mi2 = 0; Fa2 = 0; Sol2 = 0; La2 = 0; Si2 = 0; DoHigh2 = 0;
Do3 = 0; Re3 = 0; Mi3 = 0; Fa3 = 0; Sol3 = 0; La3 = 0; Si3 = 0; DoHigh3 = 0;
end