How do I convolve a transfer function with a rect function?

30 views (last 30 days)
I made this simple low pass filter (the transfer function "h") and I wanted to see the output signal when I pass a square rectangular pulse. My plan was to multiple the rect pulse in frequency domain with the transfer function and plot the output, but I kept getting an error.
Blow is what I had in mind:
%simple low pass filter with cut-off frequency of 1GHz and gain of 1v/v
f = 1E9;
w = 2*pi*f;
h = tf([0 w], [1 w]); %transfer function
%make a impulse in the time domain and transfer it to freq domain
step = 3E-16; %above this val my computer will become a drone
fs = 1/step; %sampling frequency
t = 0:step: 100E-12;
T = 50E-12; %width of my signal
x = rectpuls(t-T, T);
figure(1)
plot(t,x)
grid
%perform FFT
l = length(x); %lenght of my signal
n = 2^nextpow2(l); %fft only work when the l is n^2
fr = fs/n; %frequency resolution
fn = fs/2; %everything after fn is in the negative spect
X = fft(x,n); %this will plot
ff = -fs/2: fr : fs/2-1;
Xshifted = abs(fftshift(X));
figure(2)
plot(ff, h*Xshifted); %~~~~~~~~~~~~~~~~I am getting an error here~~~~~~~~~~~~~
How can I plot the frequency response of my system h due to the rect pulse?

Accepted Answer

Walter Roberson
Walter Roberson on 3 Jun 2020
Use symbolic calculations. The transfer function would be
syms s
syms t real
Pi = sym(pi);
f = 1E9;
w = 2*pi*f;
h = w/(s+w); %already in laplace form
Meanwhile the rectangular pulse would be
T = sym(50)*1e-12; %width of my signal
x = heaviside(t-T/2) - heaviside(t-3*T/2);
xLap = laplace(x,t,s);
And convolve them:
conv_xh = h * xLap; %convolve by multiplying laplace forms
Transfer back to time domain:
hX = simplify( ilaplace(conv_xh, s, t) );
and plot
fplot(hX, [0 100E-12])
  2 Comments
Maram Alnahhas
Maram Alnahhas on 4 Jun 2020
Thank you. You code actually makes sense and looks neat. Why wouldn't the rect function work? Also, is it necessary to shift the pulse?
Walter Roberson
Walter Roberson on 4 Jun 2020
x = heaviside(t-T/2) - heaviside(t-3*T/2);
describes a pulse that goes from time 1/2 * T to time 3/2 * T so it is already shifted.
rect() does not work because you are working with different domains. tf() emits an object of class "tf". h*Xshifted is multiplying the transfer function by a vector of 524288 numeric values, each of which acts as a gain (because that is what multiplying a tf by a constant is defined to mean, to apply a gain to it), and the result is going to be a vector of 524288 transfer functions. I don't think you want to do a bode plot of 524288 different functions.
Your vector Xshifted is a vector of per-frequency gains, each corresponding to one ff value. You would need to somehow "evaluate" the transfer function at the corresponding ff entry, and multiply the result by the Xshifted value to get the convoluted result:
[h_mag, h_phase] = freqresp(h, ff);
h_resp = h_mag(:) .* exp(1i .* h_phase(:));
convolved = Xshifted(:) .* h_resp;
plot(ff, abs(convolved)) %caution, convolved is complex-valued
pause(2)
restored = ifft(fftshift(convolved),length(t));
plot(t, [real(restored), imag(restored)]) %caution restored is complex-valued

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!