How to fix this error in interp1?

this is my code, i want to zoom in the db from -5 to -1, there is a problem that error using interp1 X mush be a vector, could you please help me?
R = 1e3; % resistor value [Ohms]
C = 1e-6; % Capacitor value [Farads]
H = tf([R*C,0], [R*C,1]);
% plot a bode plot (3dB frequency should be at 1/RC = 1000 rads/sec.
[mag,phase,wout] = bode(H);
zoom y lim = [-5,-1]; % y-axis range for zoom in section
min max w zoom = interp1(20*log10(mag),wout,zoom y lim);
indexOfInterest = (wout > min(min max w zoom)) & (wout < max(min max w zoom));
plot(wout(indexOfInterest),20*log10(mag(indexOfInterest)));

댓글 수: 2

zoom y lim = [-5,-1]
This is no valid Matlab code: variables cannot contain spaces.
if i want to plot a figure to show the gain(H = tf([R*C,0], [R*C,1])) of filter like this (green line), how to do that
part of the code is:
y_vec = lsim(H,x_vec,t_vec);
% Compute the input and outputs in the frequency domain
in_f = fft(x_vec)/length(x_vec);
out_f = fft(y_vec)/length(x_vec);
resp = out_f./in_f;
resp(abs(out_f)<1e-4) = nan;
some variables are mean as follows: x_vec in this figure ocmbine two signals, but i just want to plot a figure that the gain changes with frequency

댓글을 달려면 로그인하십시오.

답변 (1개)

Chunru
Chunru 2021년 11월 16일
편집: Chunru 2021년 11월 17일

1 개 추천

R = 1e3; % resistor value [Ohms]
C = 1e-6; % Capacitor value [Farads]
H = tf([R*C,0], [R*C,1]);
% plot a bode plot (3dB frequency should be at 1/RC = 1000 rads/sec.
[mag,phase,wout] = bode(H);
wout = squeeze(wout);
mag = squeeze(mag);
plot(wout, 20*log10(mag), 'b:');
zoom_y_lim = (-5:.1:-1); % y-axis range for zoom in section
w_zoom = interp1(20*log10(mag), wout, zoom_y_lim);
whos
Name Size Bytes Class Attributes C 1x1 8 double H 1x1 1281 tf R 1x1 8 double mag 55x1 440 double phase 1x1x55 440 double w_zoom 1x41 328 double wout 55x1 440 double zoom_y_lim 1x41 328 double
hold on
plot(w_zoom, zoom_y_lim, 'r-', 'Linewidth', 2);
ylim([-8 0])
xlim( [0 1]*1e4)
figure
semilogx(wout, 20*log10(mag), 'b'); grid on
ax = axes('Pos', [.4 .2 .4 .4], 'Box', 'on');
semilogx(w_zoom, zoom_y_lim); grid on

댓글 수: 9

Huijia Ma
Huijia Ma 2021년 11월 17일
Thank you very much! So, i want to get a figure like this, how to do this?
Chunru
Chunru 2021년 11월 17일
See the updated.
Huijia Ma
Huijia Ma 2021년 11월 17일
really appreciate you
Huijia Ma
Huijia Ma 2021년 11월 18일
could you please tell me: if i want to plot a figure to show the gain(H = tf([R*C,0], [R*C,1])) of filter like this, how to do that
part of the code like this:
y_vec = lsim(H,x_vec,t_vec);
% Compute the input and outputs in the frequency domain
in_f = fft(x_vec)/length(x_vec);
out_f = fft(y_vec)/length(x_vec);
resp = out_f./in_f;
resp(abs(out_f)<1e-4) = nan;
First find two measurement points:
[~, i1] = min(abs(f_vec - F1));
[~, i2] = min(abs(f_vec - F2));
Then plot the measurement response:
hold on
plot(fvec([i1 i2]), 20*log10(abs(resp([i1 i2]))), 'ro'); % check if u need log10
Huijia Ma
Huijia Ma 2021년 11월 18일
sorry, i don't what u mean? could you please tell me how to plot the green line. thank you
Huijia Ma
Huijia Ma 2021년 11월 19일
could you please explain what the min(abs(f_vec - F1)) mean? thanks
Chunru
Chunru 2021년 11월 20일
Let's assume that f_vec=[0 1 2 3 4 5 6 7]; F1=4.99; min(abs(f_vec-F1)) is to find the closest point in f_vec to F1. In this case, it is f_vec(6)=5 which is closest to 4.99. The whole statement of [~, i1]=min(abs(f_vec-F1)) will assign the index 6 to i1.
Huijia Ma
Huijia Ma 2021년 11월 20일
I see. Thank u very much. Do you know how to plot the green line

댓글을 달려면 로그인하십시오.

태그

질문:

2021년 11월 16일

댓글:

2021년 11월 20일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by