% lab1_freq_mod.m
%
% Matlab script to create Bode plots of frequency response.
% Rich Kozick, ELEC 226

% This program is for an RC low-pass filter with analytical frequency response
% H(w) = 1 / (1 + (j*w*R*C))

% Define circuit element values for cutoff frequency = 10 kHz
R = 1e3;   % Ohms
C = 1.5915e-8;  % Farads

wc = 1/(R*C);
fc = wc/(2*pi);
fprintf('Cutoff frequency = %f rad/s = %f Hz\n', wc, fc);

% Create a vector of frequency values in Hz to evaluate the response.
% Space them logarithmically to create a Bode plot.

f = logspace(2, 8, 500);	% Hz, from 10^2 to 10^8 with 500 points
w = 2*pi*f;			% frequency in radians/sec

H = 1 ./ (1 + j*w*R*C);	% Complex response, use ./ to divide vectors
Mag = abs(H);			% Magnitude or amplitude gain
MagdB = 20*log10(Mag);		% Gain in dB
Phase = angle(H)/pi*180;    % Phase in degrees

figure(1)
semilogx(f, MagdB, 'k-');
title('Frequency Response Magnitude')
xlabel('Frequency (Hz)');
ylabel('dB');
grid   % Remove this line to turn off grid lines

figure(2)
semilogx(f, Mag, 'k-');
title('Frequency Response Magnitude')
xlabel('Frequency (Hz)');
ylabel('Gain');
grid   % Remove this line to turn off grid lines

figure(3)
plot(f, Mag, 'k-');
title('Frequency Response Magnitude')
xlabel('Frequency (Hz)');
ylabel('Gain');
grid   % Remove this line to turn off grid lines

figure(4)
semilogx(f, Phase, 'k-');
title('Frequency Response Phase')
xlabel('Frequency (Hz)');
ylabel('Phase (degrees)');
grid   % Remove this line to turn off grid lines
axis([1e2, 1e6, -90, 0]);   % Set range for axes
