% Averaging
% K. A. Connor 23 Oct 15

% Time Range
t=3e-3.*[0:.001:1];NN=numel(t);

% Frequencies
f1=1000;f2=30000;
w1=2*pi*f1;w2=2*pi*f2;

% Magnitudes
V1=1;V2=.2;

% Phases
ph1=0;ph2=0.2;

% Voltage Functions
U1=V1.*cos(w1.*t+ph1);
U2=V2.*cos(w2.*t+ph2);
V=U1+U2;

% Plotting
plot(t,V,t,mean(V),'Linewidth', 2); 
grid;
xlabel('Time');ylabel('Voltage');title('Two Signals')

% Padding V for the Average
Vp=[V(4) V(3) V(2) V(1) V V(NN) V(NN-1) V(NN-2) V(NN-3)]; 

% Averaging
for n=1:NN;Vav(n)=(Vp(n)+Vp(n+1)+Vp(n+2)+Vp(n+3)+Vp(n+4)+Vp(n+5)+Vp(n+6)+Vp(n+7)+Vp(n+8))/9;end

pause; % Hit any key to continue
% More Plotting
figure;
plot(t,Vav,'Linewidth',2);
line([t(1) t(end)], [mean(Vav) mean(Vav)], 'Linewidth', 2)
grid;
xlabel('Time');ylabel('Voltage');title('Two Signals with Averaging')
  
pause;% Hit any key to continue
figure;
plot(t,V,t,Vav,'Linewidth',2);
line([t(1) t(end)], [mean(V) mean(V)],'Linewidth', 2)
grid;
xlabel('Time');ylabel('Voltage');title('Two Signals with and without Averaging')

