% /home/dimarzio/Documents/working/12229/lectures/rc2students.m
% Thu Oct 19 15:09:09 2017
% Chuck DiMarzio
tmax=1024;t=[1:tmax]; % Time in units of dt 

% A couple of simple functions to use
sine256=sin(2*pi*t/256); % period is 256 sec, f=(1/256) Hz
square=double(sine256>0); % Easy way to make a square wave.

%  I will explain the radar signal in class.  In this case, the
%  object is coming toward the radar, arriving (distance=0) at time
%  tmax. 
speed=40;noisedist=80; % Simulated police radar signals; 40 meters
                       % per second is kind of fast.
distance1=speed*(max(t)-t); % Ideal signal
distance2=distance1+noisedist*randn(size(t)); % Noisy signal
ttest=double(rand(size(t))>0.99);  % Noisy signal with dropouts
distance3=distance2.*(1-ttest)+tmax*speed*rand(size(t)).*ttest;

a=0.1; % a = dt/(RC)

% Periodic signals
figure;measurement=rcfilter(t,square,a);
figure;measurement=rcfilter(t,sine256,a);

% Try making a smaller and see what happens.


% -------------------------------------------------------------------

% Radar signal

% Plot the noisy distance measurement and filtered one.
figure;measurement=rcfilter(t,distance2,a);
title('Additive Noise');

% Range rate is distance difference divided by time difference 
figure;plot(t(2:end),distance2(2:end)-distance2(1:end-1),...
     t(2:end),measurement(2:end)-measurement(1:end-1));grid on;
xlabel('t, time, sec');ylabel('v, Speed, m/s');
title('Additive Noise');

figure;measurement=rcfilter(t,distance3,a);
title('Salt-and-Pepper Noise');
figure;plot(t(2:end),distance3(2:end)-distance3(1:end-1),...
     t(2:end),measurement(2:end)-measurement(1:end-1));grid on;
xlabel('t, time, sec');ylabel('v, Speed, m/s');
title('Salt-and-Pepper Noise');

% Fix the speed scale to show reasonable values
figure;plot(t(2:end),distance3(2:end)-distance3(1:end-1),...
     t(2:end),measurement(2:end)-measurement(1:end-1));grid on;
xlabel('t, time, sec');ylabel('v, Speed, m/s');
moose=axis;axis([moose(1:2),-100,100]);
title('Salt-and-Pepper Noise');

% Try different filters (ie. change a).  A long time constant
% provides a smoother distance curve which gives better velocity
% measurements, but will take longer to respond to changes in
% speed. 




