Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
270 views

MATLAB Code For Musical Notes Recognition

This MATLAB code analyzes an audio signal to recognize musical notes. It begins by initializing note names and frequencies. It then divides the audio file into frames and applies a Hamming window and FFT to each frame. It analyzes the frequency bands of each frame and identifies the note that matches the frequency band with the maximum value. If the maximum value exceeds a threshold, it prints the corresponding note name. It also identifies the harmonic frequencies present for each identified note.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
270 views

MATLAB Code For Musical Notes Recognition

This MATLAB code analyzes an audio signal to recognize musical notes. It begins by initializing note names and frequencies. It then divides the audio file into frames and applies a Hamming window and FFT to each frame. It analyzes the frequency bands of each frame and identifies the note that matches the frequency band with the maximum value. If the maximum value exceeds a threshold, it prints the corresponding note name. It also identifies the harmonic frequencies present for each identified note.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

MATLAB code for musical notes

recognition
clear;
clc;
close all;
% Note Recognition
%% Note Initialization
mainNames = char('C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#',
'B');
names = char('A0', 'A#0' ,'B0');
A0 = 27.5;
ind = 4;
for i = 0:87
data(i+1) = A0 * (2^(1/12))^i;
if i>2
a = [mainNames( rem((ind - 4), 12)+1,:) num2str(fix((ind - 4)/12)+1)];
names = char(names, a);
ind = ind + 1;
end
end
% Band Initialization
bands(1) = 20;
for i = 1:87
bands(i+1) = +7+(data(i) + data(i+1))/2;
end
bands(89) = 4500;
%%
fileName = 'c.wav'; % File name
[y, Fs] = audioread(fileName); % Read audio file
y = (y(:,1) + y(:,2))*4; % Decrease 2 channels to 1
%y = (y(:,1));
y(1:2:end) = 0; %from big vector take only odd index values % Do
decimation(decreace the no of samples)
frameLength = 4410*2; % 2 Güzel oldu
endPart = frameLength*ceil(length(y)/frameLength); % complete the last frame
%ceil is to round off to nearest integer
y(length(y)+1 : endPart) = 0;
f = linspace(1,Fs,frameLength); %creates linearly spaced vector
%%
harmonics = 0;
for i = 1:round(length(y)/frameLength) % For each frame
% Divide audio into frames
frames(i, 1:frameLength) = y( (frameLength*(i-1)+1):(frameLength*i) )';

frame = y( (frameLength*(i-1)+1):(frameLength*i) )';


frame = frame .* hamming(length(frame))'; % Hamming Window
fframe = abs(fft(frame)); % FFT
fp = sum(fframe);
p = sum(abs(frame));
b = true;
if(p < 200 || fp < 1000) % Put a threshold for processing
b = false;
end

% Bands
for i=1:88
freqBand(i) = mean(fframe(
round(bands(i)/(Fs/frameLength) ):round(bands(i+1)/(Fs/frameLength))))^2;
end

% Plotting
subplot(3,1,1)
stem(freqBand)
subplot(3,1,2)
plot(fframe)
xlim([0,500])
subplot(3,1,3)
plot(fframe)
ylim([-1 1])

hold off
pause(0.1)
sound(fframe,Fs)

% Desicion
m = find(freqBand == max(freqBand(:)));
if(b) disp(names(m,:)); % Print the result
else disp('.'); end

if(b)
index = 1;
for i = 1:88
if(freqBand(i) > 2000)
harmonics(index) = i;
index = index+1;
end
end

end
end

You might also like