码迷,mamicode.com
首页 > 其他好文 > 详细

我们为什么用卷积? Why should we use convolution?

时间:2014-09-11 20:59:12      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:filter

Why should we use convolution?



问题限定: 仅对离散信号做分析


首先要回答什么是卷积的问题.


              In mathematics and, in particular, functional analysisconvolution is a mathematical operation on two functions f and g, producing a third function that is typically viewed as a modified version of one of the original functions, giving the area overlap between the two functions as a function of the amount that one of the original functions is translated


                                                                                                                          —— wikipedia


对于卷积这个东东,一般看到这两个字的人就头大,往往望而生畏.

上述定义严谨,而《The sciencetist and engineer‘s guide to DSP》有更为简单直接的描述


Convolution is a mathematical way of combining two signals to form a third signal.


                 Convolution is a formal mathematical operation, just as multiplication, addition, and integration. Addition takes two numbers and produces a third number, while convolution takes two signals and produces a third signal.

不要有太大的心理压力,它不过就是一种数学计算操作而已,用两个输入信号合成第三个信号输出.至于具体的怎么操作,后面一步步讲清楚.


先抛出个问题,然后借助解答这个问题去回答“what is convolution ?".


这里有两组很简单信号x[n], h[n],这两组信号做卷积处理之后得到y[n]

bubuko.com,布布扣

问题来了,x[n] 和 h[n]怎么操作就可以得到y[n]呢?


一步步分解

第一步,图1,x[0]h[n-0] ,这里表达的就是把x[0]依次 和h[0]~h[3]做乘法,把结果输出到此时对应的output[n](图中的方块)

第二步,图2,x[1]h[n-1] ,这里表达的就是把x[1]依次 和h[0]~h[3]做乘法,把结果输出到此时对应的output[n](图中的方块)

第三步,图3,x[2]h[n-2] ,这里表达的就是把x[2]依次 和h[0]~h[3]做乘法,把结果输出到此时对应的output[n](图中的方块)

... ...

第九步,图9,x[8]h[n-8] ,这里表达的就是把x[8]依次 和h[0]~h[3]做乘法,把结果输出到此时对应的output[n](图中的方块)

bubuko.com,布布扣

So ? 看出规律了?

之前我故意没有给出卷积的公式定义

现在再看卷积的公式定义一点清楚得多!

bubuko.com,布布扣


用Octave(或者 matlab)实现一个demo

利用了这幅图的数据

bubuko.com,布布扣

bubuko.com,布布扣

%code writer	:	EOF
%code date	:	2014.09.11
%e-mail		:	jasonleaster@gmail.com
%code file	:	convolution_input_side.m
%code purpose:
%             A demo for convolution.
clear all
close all

x = [0 -1 -1.2 2 1.4 1.4 0.8 0 -0.8];
h = [1 -0.6 -0.4 -0.1];

length_x = size(x,2);
length_h = size(h,2);

output = zeros(1,length_h+length_x);

figure(1);
subplot(121);
scatter(1:length_x,x,'r','filled');
title('x[n]');
subplot(122);
scatter(1:length_h,h,'g','filled');
title('h[n]');

for tmp_1 = 1:length_x

	for tmp_2 = 1:length_h
	
		output(tmp_1+tmp_2-1) = output(tmp_1+tmp_2-1) + x(tmp_1)*h(tmp_2);
	end
end

figure(2);
scatter(1:size(output,2),output,'filled');
title('output[n]');



到此,回答了什么是卷积.或者说更深刻体会了什么是卷积


为什么我们要用卷积这种东东?


 

Why should we use convolution? God said I just want to make filter more simple. : )


时域中的卷积就对应于频域中的乘积,那么频域中的卷积对应于时域中的乘积


卷积为滤波而生.


这里是两个利用卷积做滤波器的例子

bubuko.com,布布扣


图a是低通滤波,图b是高通滤波,一开始我也纳闷Input signal确实复合了多个频率的信号,怎么耶和中间的信号做卷积之后就有滤波的效果了呢?

bubuko.com,布布扣

低通滤波做卷积的这个信号有什么特点吗??

filter = [0 0.1 0.16 0.25 0.25 0.16 0.1 0]*0.125;
scatter(1:8,real(fft(filter)),‘filled‘);

假设图中的数据是filter(我自己模拟的数据)

可以用频谱分析发现

这货的频谱是低频部分高的!抑制高频部分

bubuko.com,布布扣

把两个输入信号都变换到频率领域里面去,然后想乘,再把结果逆变换到时间领域不久得到低通滤波的结果了么!



这里再给出一种低通滤波的demo

首先,利用一下两个信号合成一个输入信号,模拟带高频噪声的输入

bubuko.com,布布扣

利用上面两个信号,我模拟合成了下面第一幅图的信号,作为带高频噪声的输入信号,而下面的第二幅图就是我们的做卷积的另外一个信号(低通滤波就靠它了!)

bubuko.com,布布扣

将以上两个信号做卷积得到输出信号

漂亮!几乎完美的滤去的高频噪声.

bubuko.com,布布扣


实现:

%code writer	:	EOF
%code date	:	2014.09.11
%e-mail		  :	jasonleaster@gmail.com
%code file	:	convolution_input_side.m
%code purpose:
%        Just a demo for low-pass filter by convolution.


clear all
close all

% 'y' is a simulation for original pure signal
y = sin(0.5*pi*[0:0.05:8]); 
% 'u' is a simulation for noise.
u = 0.2*sin(8*pi*[0:0.05:8]);

figure(1);
subplot(211);
plot(1:size(y,2),y);
title('original pure signal'); %BUG, I don't know why I can't write title on this figure.
subplot(212);
plot(1:size(u,2),u);
title('noise');

input_signal = y + u;

x = input_signal;
h = [1 1 1 1 1 1 1 1 1 1]*0.1;

length_x = size(x,2);
length_h = size(h,2);

output_signal = zeros(1,length_h+length_x);

figure(2);
subplot(211);
plot(1:length_x,x);
subplot(212);
scatter(1:length_h,h,'filled');

for tmp_1 = 1:length_x

	for tmp_2 = 1:length_h
	
		output_signal(tmp_1+tmp_2-1) = output_signal(tmp_1+tmp_2-1) + x(tmp_1)*h(tmp_2);
	end
end

figure(3);
plot(1:size(output_signal,2),output_signal);
title('Output signal');




哈哈是不是想看看这个家伙的频谱图呢?

bubuko.com,布布扣


怎么它就可以低通滤波捏?下面给出了上面那个家伙的频谱图,看!低通滤波!只有在低频地带有赋值为1,其他的都是0!

bubuko.com,布布扣


当我们想要什么类型的滤波器,我们可以先构想频谱表达,然后通过逆向傅立叶变换(实际上是逆向FFT),然后得到和输入信号做卷积的因子,然后做卷积,即可实现滤波!



 

Why should we use convolution? God said I just want to make filter more simple. : )




我们为什么用卷积? Why should we use convolution?

标签:filter

原文地址:http://blog.csdn.net/cinmyheart/article/details/39209129

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!