Contents

Lab 11th March 2009

% programmer: Aaron Klapheck
% Date: 11-Mar-09
clear, clc

Signal Sampled at 1000 Hz in Time Domain

f_s = 1000;
f_nyquist = f_s/2;  % By definition

load lab_data.mat

len_x = length(x)
len_t = length(t)

plot(t, x), xlabel('Time'), ylabel('Output'), ...
    title('Signal Sampled at 1000 Hz in Time Domain')
len_x =

        1001


len_t =

        1001

Frequency Content of Signal Sampled at 1000 Hz.

% Decide which exponant of 2 to use to make the fft go faster.
% This involves changing the sample frequecy to a larger number.
% This change of the sample frequency will change the look of the fft plot
% but the peaks which tell us where the natural frequencies are present will
% not change depending on fft_freq. To test this out set fft_freq equal to
% 100 or any other similar value and view the graph before and after.
exp = log(f_s)./log(2);
fft_freq = 2^(ceil(exp));


x_fft = fft(x, fft_freq);
x_fft = abs(x_fft);     % Eliminate immaginary componant
x_fft = x_fft(1:(fft_freq/2)); % Plot from 0 to .5*f_s

frequency_vector = f_nyquist*linspace(0,1,(fft_freq/2));


plot(frequency_vector(1:25), x_fft(1:25)), ...
    xlabel('Frequency'), ylabel('Frequency Content'), ...
    title('Frequency Content of Signal Sampled at 1000 Hz')

Regression to find best curve fit with polyfit

p = polyfit(t,x,20);
y = polyval(p,t);
len_y = length(y)

plot(t, x, t, y), title('Graph Polyfit Signal on Top of Original Signal'), ...
    xlabel('Time'), ylabel('Output'), ...
    legend('Original Signal', 'Polyfit Signal')
Warning: Polynomial is badly conditioned. Add points with distinct X
         values, reduce the degree of the polynomial, or try centering
         and scaling as described in HELP POLYFIT.

len_y =

        1001

Regression to find best curve fit with sines and cosines

s1 = 20*2*pi; s2 = 10*2*pi; s3 = 4*2*pi; s4 = 1*2*pi;
f1 = sin(s1*t)';
f2 = sin(s2*t)';
f3 = sin(s3*t)';
f4 = sin(s4*t)';

A = [f1, f2, f3, f4];

c = (A'*A)^(-1)*A'*x';

z = c(1)*f1 + c(2)*f2 + c(3)*f3 + c(4)*f4;

plot(t,z), title('Graph Regression Signal'), ...
    xlabel('Time'), ylabel('Output'), ...

Graph Regression Signal on Top of Original Signal

plot(t,x,t,z), title('Graph Regression Signal on Top of Original Signal'), ...
    xlabel('Time'), ylabel('Output'), ...
    legend('Original Signal', 'Regressed Signal')

finding the correlation coeficiant

x_avg = mean(x);
to = 0;
for index = 1:len_x
    diff = (x(index)-x_avg)^2;
    to = to + diff;
end
sig_x = (to/(len_x -1))^(1/2)

tot = 0;
for index = 1:len_x
    diff = (x(index)-z(index))^2;
    tot = tot + diff;
end
sig_xz = (tot/(len_x -1))^(1/2)

r = (1-sig_xz^2/sig_x^2)^(1/2)
sig_x =

   14.6088


sig_xz =

    2.8878


r =

    0.9803

FFT of regression signal vs. original signal

% Decide which exponant of 2 to use to make the fft go faster.
% This involves changing the sample frequecy to a larger number.
% This change of the sample frequency will change the look of the fft plot
% but the peaks which tell us where the natural frequencies are present will
% not change depending on fft_freq. To test this out set fft_freq equal to
% 100 or any other similar value and view the graph before and after.
exp = log(f_s)./log(2);
fft_freq = 2^(ceil(exp));


x_fft = fft(x, fft_freq);
x_fft = abs(x_fft);     % Eliminate immaginary componant
x_fft = x_fft(1:(fft_freq/2)); % Plot from 0 to .5*f_s

z_fft = fft(z, fft_freq);
z_fft = abs(z_fft);     % Eliminate immaginary componant
z_fft = z_fft(1:(fft_freq/2)); % Plot from 0 to .5*f_s

frequency_vector = f_nyquist*linspace(0,1,(fft_freq/2));


plot(frequency_vector(1:25), x_fft(1:25), frequency_vector(1:25), z_fft(1:25)), ...
    xlabel('Frequency'), ylabel('Frequency Content'), ...
    title('Frequency Content of Original vs. Regressed Signal'), ...
    legend('Original Signal', 'Regressed Signal')