Created
November 11, 2019 11:01
-
-
Save mhamilt/93093dfbb50add23bdfb4f4f89e9dccb to your computer and use it in GitHub Desktop.
Pitch Detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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