Question

I have an audio file , which represent the sound of a motor running at 2500rpm my aim is to get the period of this signal, so I can automaticlly tell what is the motor speed is. To do that I take a part of the signal and run get it autocorrelation , hopping that this willl tell the period of the singal! but I just don't get it : here is a part of my code :

clear;
clc;
[x0,Fs] = audioread('_2500.wav');
x= x0(1:2000,1);
xc = xcorr(x);
clf; 
subplot(3,1,1);
plot(x);

subplot(3,1,2);
plot(xc);

[peaks,locs] = findpeaks(xc);
hold on 
subplot(3,1,3)
plot(xc(locs),'ro');

and here are the plot :

image

and how should I consider the sampling frequency, which is : 44100 ?

Was it helpful?

Solution

You can use the autocorrelation or FFT of the signal to find where is the maximum:

% Parameters
Fc = 1e1;
Fs = 1e3;

% Signal
t = 0:1/Fs:1;
x = sin(2*pi*Fc*t);

% FFT
Y = abs(fft(x));
[~,I] = max(Y(1:floor(end/2)));

% Frequency and period
F = I-1;
T = 1/F;

% Plot
figure; 
subplot(2,1,1); plot(t,x);
subplot(2,1,2); plot(Y);
disp(['The frequency is ',mat2str(F),'Hz, and the period is ',mat2str(T),'sec.']);

This and this post are related.

OTHER TIPS

To go from your auto-correlation function xc to an estimate of the fundamental frequency, do:

fs = 44100; % Unit: Hz
xc = xc((length(xc) - 1) / 2 + 1: end); % Get the half on the positive time axis. 
[~, pidx] = findpeaks(xc);
period = (pidx(1) - 1) / fs;
F0 = 1 / period; % Estimated fundamental frequency. 

Note that there are other potentially more robust fundamental frequency / pitch estimation algorithms. Doing a google scholar search on "fundamental frequency estimation" or "pitch estimation" will lead you to some good reviews.

you find all peaks using "findpeaks" funciton, now compute the difference between each peak

P=diff(locs)

your period can be :

max(P)

The peiod of 250hz sine at 22050 sample rate, is about 88, the Frequency of your signal is equivalent at the Period if you do (Fs/Period) == Frequency

If you Know the frequency of your signal you can find the period just do Fs/Frequency

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top