Fourier transform using Convolution

I have two signals x(t) = sin(2.*pi.*t)/(pi.*t) and y(t) = x(t) I want to calculate z(t) = x(t)*y(t) and z(JW).I should plot x(t), x(JW), y(t), y(JW) and z(t), z(JW) using subplot. z(JW)=(1/(2pi))*(convolution(x(t),y(t))), I have the following code:w = [-6.*pi 6.*pi];
syms x(t)
x(t) = sin(2.*pi.*t)./(pi.*t);
subplot(3,2,1)
fplot(t,x(t));
title('x(t) vs t');
xlabel('time');
ylabel('x(t)')
X_J_W = fourier(x(t));
subplot(3,2,2)
fplot(X_J_W,w);
title('X(JW) vs w')
ylabel('X(JW)')
xlabel('W')
syms y(t)
y(t) = sin(2.*pi.*t)./(pi.*t);
subplot(3,2,3)
fplot(t,y(t));
title('y(t) vs t');
xlabel('time');
ylabel('y(t)')
Y_J_W = fourier(y(t));
subplot(3,2,4)
fplot(Y_J_W,w);
title('Y(JW) vs w')
ylabel('Y(JW)')
xlabel('W')
syms z(t)
z(t) = x(t).*y(t);
subplot(3,2,5)
fplot(z(t));
C_X_Y = conv(X_J_W,Y_J_W,'full');
Z_J_W = (1./(2.*pi)*(C_X_Y));
subplot(3,2,6)
fplot(Z_J_W,w)
in the convolution part I get
Error using conv2
Invalid data type. First and second arguments must be numeric or logical.
Error in conv (line 43)
c = conv2(a(:),b(:),shape);
and I do not know how to fix it.

Antworten (1)

Matt J
Matt J am 23 Dez. 2020

0 Stimmen

You must use int to implement a symbolic convolution integral. conv is for numeric convolution.

12 Kommentare

Nurhan Aydinalp
Nurhan Aydinalp am 23 Dez. 2020
Since the things I want to convolve are functions, I don't know how to convert those functions into integers.
Nurhan Aydinalp
Nurhan Aydinalp am 23 Dez. 2020
I also tried to convert the functions into integers by adding int after the fourier operation but it did not work
Matt J
Matt J am 23 Dez. 2020
I'm not sure why we are talking about converting functions to integers. A convolution is an integral. You can use the Symbolic Toolbox's int() command to implement it,
After I implemented the convolution integral I got some other errors
Error using fplot>singleFplot (line 240)
Input must be a function or functions of a single variable.
Error in fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 200)
hObj = cellfun(@(f) singleFplot(cax,{f},limits,extraOpts,args),fn{1},'UniformOutput',false);
Error in fplot>vectorizeFplot (line 200)
hObj = cellfun(@(f) singleFplot(cax,{f},limits,extraOpts,args),fn{1},'UniformOutput',false);
Error in fplot (line 166)
hObj = vectorizeFplot(cax,fn,limits,extraOpts,args);
Error in outline_for_z (line 42)
fplot(Z_J_W,w)
Matt J
Matt J am 23 Dez. 2020
We can't see what you did ...
w = [-6.*pi 6.*pi];
syms x(t)
x(t) = sin(2.*pi.*t)./(pi.*t);
subplot(3,2,1)
fplot(t,x(t));
title('x(t) vs t');
xlabel('time');
ylabel('x(t)')
X_J_W = fourier(x(t));
subplot(3,2,2)
fplot(X_J_W,w);
title('X(JW) vs w')
ylabel('X(JW)')
xlabel('W')
syms y(t)
y(t) = sin(2.*pi.*t)./(pi.*t);
subplot(3,2,3)
fplot(t,y(t));
title('y(t) vs t');
xlabel('time');
ylabel('y(t)')
Y_J_W = fourier(y(t));
subplot(3,2,4)
fplot(Y_J_W,w);
title('Y(JW) vs w')
ylabel('Y(JW)')
xlabel('W')
syms tao
syms z(t)
z(t) = x(t).*y(t);
subplot(3,2,5)
fplot(z(t));
C_X_Y = int(x(tao).*y(t-tao),tao,t);
Z_J_W = (1./(2.*pi)*(C_X_Y));
subplot(3,2,6)
fplot(Z_J_W,w)
Code as posted results in Z_J_W being a function of both t and tao, at least when I run it in 2019A. Hence fplot doesn't know which variable to plot against or what value to assume for the other.
I think that the expression for C_X_Y is not correct. I think it's integrating from tao to t wrt to t, when it should be integration from 0 to t wrt tao (also, see comment below). I think this would be the correct syntax for the integration and to make C_X_Y a function:
C_X_Y(t) = int(x(tao).*y(t-tao),tao,0,t);
However, when I run this I don't get a nice closed form expression for C_X_Y.
Similarly, you probably want
X_J_W(w) = fourier(x(t));
and the same for Y_J_W.
z(t) is coded as the product of x(t) and y(t). Then Z_J_W is coded as some scaled value of the convolution of x(t) and y(t), which doesn't really follow assuming that Z_J_W is supposed to be the Fourier transform of z(t).
Looking back at your original question, it's not clear what you're trying to do.
Is z(t) the convolution of x(t) and y(t)? In which case Z_J_W = X_J_W * Y_J_W
Or is z(t) the the product of x(t) and y(t)? in which case Z_J_w = (1/2/pi)*convolution(X_J_W(w),Y_J_W(w))
The expression for the convolution of x(t) and y(t) implies that both are zero for t < 0. But x(t) and y(t) are not defined that way, so the call to fourier just assumes that x(t) and y(t) are nonzero for t < 0.
z(t) the the product of x(t) and y(t) and Z_J_w = (1/2/pi)*convolution(X_J_W(w),Y_J_W(w)) and when I do as you say
X_J_W(w) = fourier(x(t));
Y_J_W(w) = fourier(y(t));
and
C_X_Y(t) = int(x(tao).*y(t-tao),tao,0,t);
I get a set of different errors this time
Array indices must be positive integers or logical values.
Error in sym/privsubsasgn (line 1128)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 965)
C = privsubsasgn(L,R,inds{:});
Error in outline_for_z (line 11)
X_J_W(w) = fourier(x(t));
Matt J
Matt J am 24 Dez. 2020
Bearbeitet: Matt J am 24 Dez. 2020
Seems alright to me:
syms x(t) y(t) z(t) c(t) X(w) Y(w) tau
x(t) = sin(2.*pi.*t)./(pi.*t);
y(t) = sin(2.*pi.*t)./(pi.*t);
z(t)=x(t).*y(t);
X(w) = fourier(x(t));
Y(w) = fourier(y(t));
c(t)=int(x(tau).*y(t-tau),tau,-inf,+inf) %convolution of x and y
c(t) = 
Nurhan Aydinalp
Nurhan Aydinalp am 24 Dez. 2020
Bearbeitet: Nurhan Aydinalp am 24 Dez. 2020
When I try to plot c(t) I do not get a graph.
syms x(t) y(t) z(t) c(t) X(w) Y(w) tau
x(t) = sin(2.*pi.*t)./(pi.*t);
y(t) = sin(2.*pi.*t)./(pi.*t);
z(t)=x(t).*y(t);
X(w) = fourier(x(t));
Y(w) = fourier(y(t));
c(t)=int(x(tau).*y(t-tau),tau,-inf,+inf);
fplot(c(t),t
I get the following error
Warning: Reached the limit on the maximum number of intervals in use. Approximate bound on error is
4.1e-06. The integral may not exist, or it may be difficult to approximate numerically to the requested
accuracy.
Truncating the convolution seems to help:
syms x(t) y(t) z(t) c(t) X(w) Y(w) tau
x(t) = sin(2.*pi.*t)./(pi.*t);
y(t) = sin(2.*pi.*t)./(pi.*t);
z(t)=x(t).*y(t);
X(w) = fourier(x(t));
Y(w) = fourier(y(t));
c(t)=int(x(tau).*y(t-tau),tau,-100,+100);
fplot(c(t))
Paul
Paul am 24 Dez. 2020
Bearbeitet: Paul am 24 Dez. 2020
Nurhan,
Why compute the convolution of x(t) and y(t)? I thought the problem at hand is related to the product of x(t) and y(t).
If z(t) = x(t)y(t), then
Z(w) = conv(X(w),Y(w))/2/pi:
>> syms u
>> Z(w)=int(X(u)*Y(w-u),u,-inf,inf)/2/pi;
>> Z(w)
ans =
-((heaviside(- w - 4*pi)*(w + 4*pi))/2 - w*heaviside(-w) + (heaviside(4*pi - w)*(w - 4*pi))/2)/pi
>> fplot(Z(w),[-20 20])
The result can be confirmed by numerically computing the Fourier transform of z(t):
>> fun=matlabFunction(z(t)*exp(-1j*w*t));
>> wr=-20:.1:20;
>> for ii=1:numel(wr),q(ii)=integral(@(t)fun(t,wr(ii)),-20,20);end
>> hold on
>> plot(wr,real(q),'ro'),grid

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2020b

Gefragt:

am 23 Dez. 2020

Bearbeitet:

am 24 Dez. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by