% lab1_freq.m
%
% Matlab script to plot analytical and measured frequency response.
% Rich Kozick, Fall 1997

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

% Define circuit element values
R = 1.15e3;   % Ohms
C = 0.22e-6;  % Farads

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

f = logspace(1, 5, 500);	% Hz
w = 2*pi*f;			% radians/sec

H = 1 ./ (1 + (j*w*R*C));	% Complex response
Mag = abs(H);			% Magnitude
MagdB = 20*log10(Mag);		% dB

figure(1)
semilogx(f, MagdB);
title('Frequency Response Magnitude')
xlabel('Frequency (Hz)');
ylabel('dB');
grid

% To compare measured data with analytical frequency response, both can
% be plotted on the same graph.
% Suppose you have 4 measured values (you will have more than 4):

f_meas = [10, 100, 1e3, 1e4];
Mag_meas = [0.99, 0.95, 0.4, 0.09];
MagdB_meas = 20*log10(Mag_meas);

figure(2)
semilogx(f, MagdB, '-', f_meas, MagdB_meas, 'o');
legend('Analytical', 'Measured')
title('Frequency Response Magnitude')
xlabel('Frequency (Hz)');
ylabel('dB');
grid

% You can drag the legend with the mouse to a desirable place on the plot.
% If the grid makes the plot messy, it can be omitted.
