Reputation: 35
i want write a script to plot a graph for the transfer function [H(f)] for a band pass filter, |H(f)| against frequency and the phase of H(f) (degrees) against frequency, im very new to matlab so the syntax is not 100%, im getting confused because everything is auto formatted in matrix form. below is my script:
% RCL circuit: band-pass filter
R=55590; L=0.9571; C=48.811*10.^(-9); % values of the Resistor and Capacitor
f=(0:60e3); w=2*pi*f; % frequency (f) range of measurements
H=(R./(sqrt(R^2+(w*L-(1./(w*C))).^2))); % Transfer Function
% Magnitude (absolute value) of the transfer function
plot(f,abs(H),'LineWidth',2); grid on; hold on
xlabel('Frequency [Hz]','FontSize',20); ylabel('|H(f)|','FontSize',20)
figure
plot(f,angle(H)*180/pi,'LineWidth',2); grid on; hold on
xlabel('Frequency [Hz]','FontSize',18);
ylabel('phase of H(f) [degrees]','FontSize',20)
this is the transfer function formula im using
below is another pic of what my experimental results were and an expected graph, i just dont understand why MATLAB isnt ploting what i want?
Upvotes: 2
Views: 21283
Reputation: 25232
Are you aware of the bodeplot
function?
The transfer function for a simple second order bandpass is:
You just need to insert your values into Matlab's tf
function and plot it with bodeplot
:
R = 55590;
L = 0.9571;
C = 48.811*10.^(-9);
% tf creates transfer function object
sys = tf( [R*C 0] , [L*C R*C 1]); % [R*C 0] vector of numerator coeffcients
% R*C*s + 0*1
% [L*C R*C 1] vector of denominator coeff.
% L*C*s^2 + R*C*s + 0*1
bodeplot(sys) % plot command to plot frequency response
% of magnitude and phase
and it plots:
Alternatively, if you need more outputs like magnitude and phase as variables, use bode
, the plot is equal. But bodeplot
offers more additional plot customization options.
Regarding your comment, that you need a linear axis:
you just need to add the following lines before the plot command:
P = bodeoptions; % handle to plot options
P.MagScale = 'linear';
P.MagUnits = 'abs';
bodeplot(sys,P) % plot command with custom options
so it looks as follows:
For adjusting the frequency axis limits, use:
P.XLim = [1 60e3];
or analogue for the magnitude:
P.YLim = [0 1];
I would advise against a linear frequency axis, but if you REALLY want it, you can use:
P.FreqScale = 'linear';
P.FreqUnits = 'Hz'; % optional
If you want to plot your experimental data together with the plot above, follow this example.
Use bode
to get equally formatted data from your transfer function, like your experimental data and use semilogx
to plot it.
freqVec = logspace(-1, 3, 5000);
[mag, phs] = bode(sys, freqVec * (2*pi));
mag = db(mag(:));
phs = phs(:);
figure;
subplot(211)
semilogx(freqVec, mag); hold on
semilogx(freqVec, experimentalDataMagnitude); hold off
grid on
title('System Bode Plot')
ylabel('Magnitude (dB)')
subplot(212)
semilogx(freqVec, phs); hold on
semilogx(freqVec, experimentalDataPhase); hold off
grid on
ylabel('Phase (deg)')
xlabel('Frequency (Hz)')
Upvotes: 5