plot magnitude and phase in lowpass active filter and show maximum gain in matlab in matlab

To plot the magnitude and phase of a lowpass active filter in MATLAB, you can use the 'freqs' function. Here's an example code:

main.m
% Filter specifications
R1 = 1e3; % Resistor 1 value in ohms
R2 = 1e3; % Resistor 2 value in ohms
C = 1e-6; % Capacitor value in farads

% Create the filter transfer function
num = [1];
den = [R1*R2*C^2, (R1+R2)*C, 1];
H = tf(num, den);

% Get the frequency response
w = logspace(-2, 4, 1000); % Frequency range from 0.01 to 10000 rad/s
[mag, phase] = freqs(num, den, w);

% Plot magnitude response
figure;
semilogx(w, 20*log10(abs(mag)));
grid on;
xlabel('Frequency (rad/s)');
ylabel('Magnitude (dB)');
title('Magnitude Response');

% Plot phase response
figure;
semilogx(w, unwrap(phase));
grid on;
xlabel('Frequency (rad/s)');
ylabel('Phase (rad)');
title('Phase Response');

% Find maximum gain and corresponding frequency
[max_gain, idx] = max(abs(mag));
max_freq = w(idx);

disp(['Maximum gain: ' num2str(max_gain)]);
disp(['Frequency at maximum gain: ' num2str(max_freq) ' rad/s']);
890 chars
37 lines

This code creates a lowpass active filter transfer function and calculates the frequency response by using the 'freqs' function. It then plots the magnitude and phase response using semilogarithmic scales. Finally, it finds the maximum gain and the corresponding frequency at which it occurs.

Note: You may need to adjust the filter component values (R1, R2, and C) based on your specific filter design.

gistlibby LogSnag