Skip to content

Instantly share code, notes, and snippets.

@mhamilt
Created November 11, 2019 11:01
Show Gist options
  • Save mhamilt/93093dfbb50add23bdfb4f4f89e9dccb to your computer and use it in GitHub Desktop.
Save mhamilt/93093dfbb50add23bdfb4f4f89e9dccb to your computer and use it in GitHub Desktop.
Pitch Detection
function out = autocorrolate(signal)
% AUTOCORROLATE Multiply a signal by itself over a moving delay window.
% Max delay will be equal to the length of the signal
% Y = AUTOCORROLATE(x) returns auto correlation of x
%
N = length(signal);
out = zeros(size(signal));
for l = 1:N - 1
for n = l+1:(N)
out(l) = out(l) + (signal(n)*signal(n-l));
end
end
end
function out = pitchDetect(signal, Fs)
% PITCHDETECT Detect the F0 pitch in Hz of a signal with autocorrelation
% Hz = PITCHDETECT(signal, Fs) return F0 of a signal with sample rate Fs
y = autocorrolate(signal);
local_max = find(islocalmax(y));
last_value = 0;
distances = [];
for x = local_max
distances = [distances,x - last_value];
last_value = x;
end
out = Fs / mean(distances);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment