%% Fan Switchoff
% Compute the time-synchronous average of the position of a fan blade as it slows down after switchoff.
% A desk fan spinning at 2400 rpm is turned off. Air resistance (with a negligible contribution from bearing friction) causes the fan rotor to stop in approximately 5 seconds. A high-speed camera measures the x-coordinate of one of the fan blades at a rate of 1 kHz.

fs = 1000;
t = 0:1/fs:5-1/fs;

rpm0 = 2400;

% Idealize the fan blade as a point mass circling the rotor center at a radius of 10 cm. The blade experiences a drag force proportional to speed, resulting in the following expression for the phase angle:
% \phi =2\pi f_0 T\left(1-e^{-t/T} \right),
% where f_0 is the initial frequency and T=0\ldotp 75 second is the decay time.

a = 0.1;
f0 = rpm0/60;
T = 0.75;

phi = 2*pi*f0*T*(1-exp(-t/T));

% Compute and plot the x- and y-coordinates. Add white Gaussian noise.

x = a*cos(phi) + randn(size(phi))/200;
y = a*sin(phi) + randn(size(phi))/200;

plot(t,x,t,y)

% Determine the synchronizing signal. Use the tachorpm function to find the pulse times. Limit the search to times before 2.5 seconds. Plot the rotational speed to see its exponential decay.

[rpm,~,tp] = tachorpm(x(t<2.5),fs);
tachorpm(x(t<2.5),fs)

% Compute and plot the time-synchronous average signal, which corresponds to a period of a sinusoid. Perform the averaging in the frequency domain.

clf
tsa(x,fs,tp,'Method','fft')

% Copyright 2012 The MathWorks, Inc.