Question

The original problem is fairly straight forward: "Develop a program that evaluates convolutions"

The issue is the programming, we are not allowed to use CONV function therefore we are going to utilize the Fourier transform to send it over to the frequency domain multiply the two signals together and inverse Fourier transform them back.

Conceptually that's not tough, but we aren't allowed to use any transform functions.

Finally we're left with direct analysis.

1) Integrate both signals after multiplying by an exponential function.

2) Multiply the two functions together.

3) multiply the answer by 1/(2*pi) and integrate.

4) Plot the results.

(Our teacher basically said we had to use the trapz function)

Here is the code I have so far:

t = -5:.1:5;

w = pi;

x = zeros(101,1);

h = zeros(101,1);

y = zeros(101,1);

Y = zeros(101,1);

H = zeros(101,1);

X = zeros (101,1);


for i = 1:101;

    x(i) = exp((-3*t)+(-1i*w*t));

    h(i) = exp((-2*t)+(-1i*w*t));

   X(i) = trapz(t,x);

   H(i) = trapz(t,h);

   Y(i) = X.*H;

   y(i) = (1/(2*pi))*trapz(t,Y.*exp(-1i*w*t));

end

disp (length(t))

disp (length(y))

figure(1);

subplot(1,2,1),plot(t,real(y));grid on;

Here is where I need help. I know that I need to make a zero vector for y, but as of yet I'm struggling with the syntax and sizing, also I am having trouble getting it outside of the for loop and plotted.

I put the index of the For Loop from 1:101 because it eliminates the need for the abs() function and it allows me to skip dealing with the zero value. Great! Here is the next issue:

x(i) = exp((-3*t)+(-1i*w*t));

The x vector is still filled with zeros at the end of the loop, and isn't being assigned values from the equation. Why is this? (This is also the case with h) These two issues are snowballing throughout the rest of the code. Side question: Is there a way to account for a zero index like the abs() works for negatives?

I need more than ten reputation to post more than two links... so I can't link the pages I've gone through but basically Zero Matrix, Plotting, Convolution, Plotting data from a For Loop, and Zero Matrix Maximum Variable size.

If there is any other information that I can provide just ask, also if there is a better way to accomplish this goal please say so, and provide an in depth explanation.

Thanks for the help.

Was it helpful?

Solution

You can generate a zero-vector/matrix and store it in yt using the command

yt = zeros(r,c)

where r and c are the rows and columns of the matrix, respectively.

Another issue seems to be the storage of results. You can save results into a vector, let's say y, by

y(idx) = [your calculations]

where idx is the loop index. This way, each result from each evaluation of the loop is stored separately for later access. You can access (for read and write) an element by its index within the vector, e.g.

y(1)
y(3)

will return the first and the third element of y in Matlab's command window.

Of course, you should preallocate y - for example by filling it with an appropriate sized vector of zeros - before starting the loop to make the program faster:

y = zeros(100,1);

where 100 is the number of elements you want to store. It'll work just fine for now without preallocation. Matlab will do the work for you.

[edit/add] First of all: good choice to use a loop index to refer to elements. However, using i is strongly discouraged, as it also refers to the imaginary unit. In general, avoid using i and j as variable names, nomatter which language you are using to program. Use ii or jj instead. Some people even use double-letter names for their loop variables, e.g. aa, bb, ii, ..

The second issue, that I see right away is the following. Two things get mixed up here: the vector-typed t and the scalar value of an element x(ii):

x(i) = exp((-3*t)+(-1i*w*t));

Once you fix the i-issue (loop variable name), Matlab will probably issue an array-dimension mismatch error. Result and variable to which the value is to be stored have to equal in type and size. You need to adress the elements within t as well, e.g.

x(ii) = exp((-3*t(ii))+(-1i*w*t(ii)))

Alternatively, Matlab allows you to circumvent the loop-construction altogether:

x = exp((-3*t)+(-1i*w*t));

I see it from the

Y = X.*H

that you already discovered the element-wise operations on vectors. These are helpful in doing all the calculations without a loop.

PS: If the suggestions were helpful for you, authors of answers (I see you posted another question and accepted an answer as solution as well) are happy to receive an up-vote. ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top