%% Fun application
% Any wave or signal can be made up of tons of sinusoidal signals at
% different frequencies. You have probably already used a function
% generator to produce a squarewave. I will create a square wave using only
% sin and cos function and an summing op amp.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% edit this code to determine the number of signals you want to use
% Don't do more than 1000, it takes a while to sum a lot of signals
% together.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
number_of_signals = 100;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% don't touch this code. But look below to edit more code.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

T = 1; %period
t= 0:.0001:2*T -.0001; % array of time
Vs = []; %an array of Vs;
Rs = []; % an array for the input resistance. I will make them all the same
Rs_value = 100; %the values of Rs
A = 1; %amplitude

%for loop to create number_of_signals input signals into the summing op amp
for m = 1:number_of_signals
   Vs = [Vs, (4*A/(m*pi)*cos(2*m*pi*t/T)*sin(m*pi/2))']; 
   Rs = [Rs, Rs_value];
end

%parameters for the summing op amp
Rf = 100;
Vpos = 20;
Vneg = -20;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Edit this code to implement your own function file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%summ op amp function
[Vout] = MT240_NR_5_4_1_Sum_opAmpFunctionFile(Vs , Rs, Rf, Vpos, Vneg);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Don't touch this code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(1);
plot(t, Vout);
xlabel('time(s)');
ylabel('Vout (v)');

