Basic questions on conv function

14 次查看(过去 30 天)
Hi,
Suppose I have the following code:
t=(-1:.1:5); %Time axis
for i=1:1:length(t);
if t(i)<0
a=0;
h(i)= 0;
f(i) = 0;
else
a=1;
h(i) = a.*exp(-2.*t(i));
f(i) = a.*exp(-t(i)./2);
end
end
u=conv(f,h,'same')*.1;
plot(t,h,t,f,t,u1)
I'm just trying to plot the convolution of these two basic functions. When I look at examples people just use conv and plot the data along with the original functions. But it doesn't seem that easy, the convolution set is larger than the original dataset, and it's shifted. By shifted I mean the plot of conv vs. t has amplitude before t=0. How does one correct for such things to display the functions and the convolution on the same graph?
Thanks a lot, Charles
  3 个评论
Angus
Angus 2013-6-25
Also this explanation and example might be helpful. The example at the end is similar to what you are doing.
Cheers
Senaasa
Senaasa 2013-6-25
Thanks for links Angus.
Unfortunately, my problem still exists. I understand how and why the convolution is larger than the original datasets. The result of conv (using full shape argument) is larger and the h function (usually the impulse response) is actually flipped, which is why I have a nonzero amp before t=0. I'm trying to figure out how to correct for this when plotting, which no one seems to address in online tutorials. I have loops which correctly calculate the convolution of my two example functions but I cannot get conv to reproduce it. Hopefully, it's something stupid I'm missing.

请先登录,再进行评论。

采纳的回答

Senaasa
Senaasa 2013-6-26
Hi all,
For those who run into this problem, I found that after you perform the full conv, you need to shift the time axis by
t = 2*-1+dt*(0:(length(u)-1));
where u is the result of the convolution. Then you can plot (t,u) on the same plot as f and h and the convolution displays correctly.
  2 个评论
Michael Reshko
Michael Reshko 2019-4-4
Could you please clarify what should be done.? Say I have two Gaussian PDFs, f, sampled on interval [-a, a]. What should be the x-axis values after conv(f,f)? Thanks
Image Analyst
Image Analyst 2019-4-4
Your first element will now corresponse to -a - a or -2a since the kernel goes more into the negative. So what was 0 no longer is at the a'th element, it's at the 2a'th element. Make adjustments if your array has more than 1 per element, but a fractional amount or something more than 1.

请先登录,再进行评论。

更多回答(3 个)

Jan
Jan 2013-6-26
The help text of doc seems to clarify, that the 'same' flag limits the output to the input length. Does this help already?

Image Analyst
Image Analyst 2013-6-26
You need to understand both what it's doing, and what you want. If you don't want data where the kernel is not completely overlapped with the main signal, then you can use the 'valid' option. If you want it to end when it's half overlapped, use the 'same' option. Most often I use the 'same' option because I want the data the same size as the original and don't really care too much what happens out near the edges. If you want the full convolution then you have to realize that the index no longer corresponds to the same part of the signal as the same number index did in the original signal. So if you're plotting, you have to take that into account.
  1 个评论
Senaasa
Senaasa 2013-6-26
Thank you for suggestions. In my above code, if I use 'same' in conv, I get a plot that is the same length as the input vectors, but when plotting it (with respect to t) the result as non zero amplitude from -1 to 5, which doesn't make sense since both functions are zero at all negative times. I'm guess this is because I'm selecting the middle of the convolution with 'same'. Also if I use valid, I get a line at zero. If you use the 'same' shape do you then have to shift the result?

请先登录,再进行评论。


sri sutha
sri sutha 2017-3-8
I have use 13 convolutional layers for text extraction . please help me to write the coding.
  1 个评论
Jan
Jan 2017-3-8
Please open a new thread for a new question. Then provide more details.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Spectral Measurements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by