[SOLVED] convolution of two sine waves

Status
Not open for further replies.

preethi19

Full Member level 5
Joined
Jun 30, 2014
Messages
273
Helped
0
Reputation
0
Reaction score
1
Trophy points
16
Activity points
3,474
Hi i am trying to convolve two sine waves of the same freq 5KHz. I am able to multiply both in time domain and when i convert i am able to get the spectrum at 10KHz. But rather i want to try for obtaining two 5KHz spectrum and convolving them and checking the result. I tried conv() function in matlab but the spectrum is wrong. Here is my matlab code. Can someone pls let me know the correction. Thank you!!!!

Code:
Fs = 150; % Sampling frequency
t = 0:1/Fs:1; % Time vector of 1 second
f = 5; % Create a sine wave of f Hz.
x = sin(2*pi*t*f);
y = sin(2*pi*t*f);
nfft = 1024; % Length of FFT
X = fft(x,nfft);
Y = fft(y,nfft);

X = X(1:nfft/2);
Y = Y(1:nfft/2);

mx = abs(X);
my = abs(Y);

f = (0:nfft/2-1)*Fs/nfft;
figure(1);
plot(t,x);
figure(2);
plot(t,y);
 % plotting both sine waves of 5KHz

figure(3);
plot(f,mx);
figure(4);
plot(f,my);
%plotting their spectrum


Later if i do simple multiplication "z=x.*y;"
i am able to multiply the waves and then take fft of it. But when i do "sp=conv(mx,my);"
i am not getting desired output spectrum. Can anyone pls tell how to go about. Thank you!!!
 

What you are trying to do with plot (f,mx); check both arguments.
 

plot(f,mx) is just to see the freq spectrum of the individual sine waves which comes out fine as 5KHz in freq spectrum. But ultimately i am trying to convolve these two 5K and 5K (in freq domain ) to get the resultant 10K spectrum.
 

Hi,

As you know, regular convolution of function1 and function2 results with the length of function1+function2 -1. And in matlab, conv(func1, func2) gives the same length. However, if you work with DFT, you use rather CIRCULAR CONVOLUTION. So try CCONV function in matlab.

And as far as I see, you use conv(mx,my) namely convolution of amplitudes of FTs. This is not correct either. You have to convolve X and Y themselves. The right one should be cconv(X,Y).

Hope this helps.

-yaman
 

I do not have matlab but I use octave. It is working as expected (the code you have pasted). Please be specific and tell what is expected and what is found.

- - - Updated - - -

You are trying the convolution of two identical functions, right? I see that mx and my are same as per the graph.
 

Yes you are right.. I am trying to convolve two identical functions and to see the result of the convolution. Just trying to see when i convolve two 5KHz signal to get result of 10KHz.
And i tried cconv(X,Y).. I am still not able to obtain a output figure of 10KHz spectrum..Thank you for the reply so far!!!!
 

I used octave and conv(X,Y,"same") returned an array with the same size as X or Y and I could plot that against t or f. Please check the documentation for conv in your setup.
 

But ultimately i am trying to convolve these two 5K and 5K (in freq domain ) to get the resultant 10K spectrum.
Did you consider the possibility that your expectation is wrong?
 


first of all, sinus signal with 5khz frequency will give you two dirac delta at 5KHz and -5KHz. similarly if you multiply two 5KHz sinus signal, you'd get two cos at the frequencies of 0KHz and 10KHz which means you would have 3 delta functions in frequency spectrum at 0, 10KHz and -10KHz. So you should understand that those frequencies(5KHz, 10KHz) are not bandwidths just will dirac deltas.

I add cconv to your code and it worked.
Code:
Fs = 150; % Sampling frequency
t = 0:1/Fs:1; % Time vector of 1 second
f = 5; % Create a sine wave of f Hz.
x = sin(2*pi*t*f);
y = sin(2*pi*t*f);
nfft = 1024; % Length of FFT
X = fft(x,nfft);
Y = fft(y,nfft);
Z= CCONV(X,Y,size(X,2));

X = X(1:nfft/2);
Y = Y(1:nfft/2);
Z= Z(1:nfft/2);

mx = abs(X);
my = abs(Y);
mz=abs(Z);

f = (0:nfft/2-1)*Fs/nfft;
figure(1);
plot(t,x);
figure(2);
plot(t,y);
 % plotting both sine waves of 5KHz

figure(3);
plot(f,mx);
figure(4);
plot(f,my);
figure (5);
plot(f,mz);
%plotting their spectrum

 
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…