|
a |
|
b/pitch_detection.m |
|
|
1 |
|
|
|
2 |
%% Signal Definition |
|
|
3 |
|
|
|
4 |
[file, path] = uigetfile('*.wav', 'Select a wave file'); |
|
|
5 |
nameoffile = fullfile(path,file); |
|
|
6 |
|
|
|
7 |
[y,fs] = audioread(nameoffile); |
|
|
8 |
y = y(:,1); |
|
|
9 |
y = y(fix(10*fs):fix(20*fs)); |
|
|
10 |
|
|
|
11 |
%% Frames and Windows |
|
|
12 |
|
|
|
13 |
frame_size=20; |
|
|
14 |
frame_shift=10; |
|
|
15 |
window_length=(frame_size/1000)*fs; |
|
|
16 |
sample_shift=(frame_shift/1000)*fs; |
|
|
17 |
no_of_windows = (floor((length(y))/sample_shift)-ceil(window_length/sample_shift)); |
|
|
18 |
f0=zeros(1,no_of_windows); |
|
|
19 |
maxamp = 0; |
|
|
20 |
flag_y= envelop_hilbert(y); |
|
|
21 |
|
|
|
22 |
%% Autocorrelation |
|
|
23 |
|
|
|
24 |
for i=1: no_of_windows |
|
|
25 |
|
|
|
26 |
sig = y(i*sample_shift:(i*sample_shift)+window_length); |
|
|
27 |
thres = flag_y(i*sample_shift:(i*sample_shift)+window_length); |
|
|
28 |
activity_flag = max(abs(thres)); |
|
|
29 |
|
|
|
30 |
if activity_flag == 1 % discarding low amplitude freq |
|
|
31 |
cor = xcorr(sig); |
|
|
32 |
[pks,locs] = findpeaks(cor); |
|
|
33 |
[midval, midloc] = max(pks); |
|
|
34 |
[maxval,maxloc] = max(pks(midloc+1:end)); |
|
|
35 |
|
|
|
36 |
if isequal(maxval,maxloc) |
|
|
37 |
f0(i) = 0; |
|
|
38 |
else |
|
|
39 |
period = (abs(locs(midloc)-locs(maxloc+midloc))); |
|
|
40 |
f0(i) = fs / period; |
|
|
41 |
end |
|
|
42 |
else |
|
|
43 |
f0(i) = 0; |
|
|
44 |
end |
|
|
45 |
|
|
|
46 |
end |
|
|
47 |
|
|
|
48 |
v = f0 .* 4.0526e-2; |
|
|
49 |
|
|
|
50 |
%% plots |
|
|
51 |
t=1/fs:1/fs:(length(y)/fs); |
|
|
52 |
|
|
|
53 |
[peaks,locs] = findpeaks(f0,'MinpeakHeight',0.5*max(f0),'MinPeakDistance',50);ylim([0 1200]);xlim([0 2000]); |
|
|
54 |
|
|
|
55 |
plot(1:no_of_windows,f0*4.0526e-2,locs,peaks*4.0526e-2,'o');title('Pitch of each window');ylabel('Velocity (cm/s)');xlabel('Windows'); |
|
|
56 |
ylim([0 100]); |
|
|
57 |
g = zoom; |
|
|
58 |
g.enable = 'on'; |
|
|
59 |
set(gcf, 'Position', get(0,'Screensize')); |
|
|
60 |
|
|
|
61 |
max_velocity = max(peaks)*4.0526e-2; |
|
|
62 |
Velocity = mean(peaks)*4.0526e-2; |
|
|
63 |
|
|
|
64 |
fprintf('\nMax Systolic Peak Velocity = %.2f cm/s\n',max_velocity); |
|
|
65 |
fprintf('\nAverage Systolic Peak Velocity = %.2f cm/s\n\n',Velocity); |