%% RPM Peak
% Load a simulated tachometer signal sampled at 300 Hz.

%%

load tacho

%%
% Compute and visualize the RPM signal using |tachorpm| with the default
% values.

tachorpm(Yn,fs)

%%
% Increase the number of fit points to capture the RPM peak. Too many
% points result in overfitting. Verify this result by zooming in on the
% area around the peak.

tachorpm(Yn,fs,'FitPoints',600)

axis([0.47 0.65 1320 1570])

%%
% Choose a moderate number of points to obtain a better result.

tachorpm(Yn,fs,'FitPoints',100)

%%
% Add white Gaussian noise to the tachometer signal. The default
% pulse-finding mechanism misses pulses and returns a jagged signal
% profile. Verify this result by zooming in on a two-second time interval.

rng default
wgn = randn(size(Yn))/10;
Yn = Yn+wgn;

[rpm,t,tp] = tachorpm(Yn,fs,'FitPoints',100);

figure
plot(t,Yn,tp,mean(interp1(t,Yn,tp))*ones(size(tp)),'+')
hold on
sl = statelevels(Yn);
plot(t,sl(1)*ones(size(t)),t,sl(2)*ones(size(t)))
hold off
xlim([9 10])

%%
% Adjust the state levels to improve the pulse finding.

sl = [0 0.75];

[rpm,t,tp] = tachorpm(Yn,fs,'FitPoints',100,'StateLevels',sl);

plot(t,Yn,tp,mean(interp1(t,Yn,tp))*ones(size(tp)),'+')
hold on
plot(t,sl(1)*ones(size(t)),t,sl(2)*ones(size(t)))
hold off
xlim([9 10])

%% 
% Copyright 2012 The MathWorks, Inc.