-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.m
86 lines (73 loc) · 2.03 KB
/
analyze.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
clc
clear all
file="mix_11s_audio-joiner.mp3";
[x,fs]=audioread(file); %read sound file to x, and sampling frequency to fs
w = notewindows(x)
num_notes=length(w)-1
Ns = length(x); % get length of the signal sequence
t = (1/fs)*(1:Ns); % get the duration of the signal knowing the sampling period 1/fs and the number of %sample points
Xk = abs(fft(x)); % do the fourier transform and take only the amplitude using abs()
%Xk = Xk(1:Ns) % take only the first half of the transform due to the symmetry about the center
f = fs*(0:Ns-1)/Ns; % generate the vector of frequencies knowing the sampling frequency and reaching fs/2
% plot the waveform of the signal
xk=Xk/max(Xk);
%plot the Fourier transform of the signal
figure(1)
subplot(2,1,1);
plot(t, x,'color','blue')
xlabel('Time (s)')
ylabel('Amplitude')
title('Input Audio Signal');
subplot(2,1,2);
plot(f, xk)
hold on
xlim([0 6500])
ylim([0,1.1])
xlabel('Frequency (Hz)')
ylabel('Normalisied Amplitude')
title('Magnitude spectrum of FFT');
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;
% Bands
for i=1:88
freqBand(i) = mean(Xk( round(bands(i)/(fs/Ns) ):round(bands(i+1)/(fs/Ns))))^2;
freqband(i)=freqBand(i);
end
% Print the result
for i=1:num_notes
m = find(freqband == max(freqband(:)));
disp(names(m,:))
freqband=freqband(freqband~=max(freqband));
%plot(f,x
end
hold off
%
% index = 1;
%
% for i = 1:88
%
% if(freqBand(i) > 2000)
% harmonics(index) = i;
% index = index+1;
%
% end
%
% end
%
%