% sinemetrics.m
%
% by Gary P. Scavone
% McGill University, 2005.

fs = 44100;                    % sampling rate
T = 1/fs;                      % sampling period
N = 1500;                      % number of samples to compute

% Generate a sinusoidal signal
f1 = 200;                      % frequency #1
omega1 = 2*pi*f1;              % angular frequency #1
x = cos(omega1*[0:N-1]*T);
plot( x, 'b' )
hold on

% Calculate signal mean (can also use mean() function)
meen = sum( x ) / N
plot( meen.*ones(1, N), 'r' )

% Calculate signal energy
energee = sum( x.^2 )
%plot( energee.*ones(1, N), 'g' )

% Calculate signal power
powwer = energee / N
plot( powwer.*ones(1, N), 'g' )

% Calculate RMS signal level
rms = sqrt( powwer )
plot( rms.*ones(1, N), 'y' )

% Calculate signal variance
variance = sum( (x - meen).^2 ) / N
plot( variance.*ones(1, N), 'm' )

hold off

legend( 'signal', 'mean', 'power', 'rms power', 'variance' );
xlabel( 'Time (samples)' );
ylabel( 'Amplitude' );