% Investigating the impact of system linearity
%   ece 300 HW 5
%   BAF Jan 2003
%
Fs=100000; dt=1/Fs; len=2^15;       % this sample rate is plenty high for our signals
                                    % 'dt' is spacing between time points, 'len' is number of time points
index=1:len;                        % array index
time=index*dt;                      % time vector

% Insert signal description here
f0=200;
x=1*cos(2*pi*f0*time);

% Plot the input signal and play the sound
%figure
subplot(2,1,1); plot(time(1:floor(4/f0/dt)),x(1:floor(4/f0/dt)) )   % plots 4 periods of the waveform
xlabel('time (s)')
ylabel('amplitude')
soundsc(x,Fs);

pause                               % press any key to continue

% Insert system description here
y = (10.1+x/10).^0.2;
%
%  code for part (i)
%
dt = time(2)-time(1);
a = 1/dt;
b = 1/dt;
y(1) = 0;
for k=2:length(time)
    y(k) = (y(k-1)+b*dt*x(k).*x(k))/(1+a*dt);
end;
%
%
% Plot the output signal and play the sound
subplot(2,1,2); plot(time(1:floor(4/f0/dt)),y(1:floor(4/f0/dt)) )
xlabel('time (s)')
ylabel('amplitude')
soundsc(y,Fs);

