mafan88的个人空间 https://blog.eetop.cn/?162539 [收藏] [复制] [分享] [RSS]

日志

Matlab中filter和conv函数的区别

已有 17978 次阅读| 2011-10-9 18:27 |个人分类:数字信号处理

【转】 Matlab中filter和conv函数的区别

MATLAB中,可以用函数y=filter(p,d,x)实现差分方程的仿真,也可以用函数 y=conv(x,h)计算卷积。

(1)y=filter(p,d,x)用来实现差分方程,d表示差分方程输出y的系数,p表示输入x的系数,而x表示输入序列。输出结果长度数等于x的长度。

实现差分方程,先从简单的说起:
filter([1,2],1,[1,2,3,4,5]),实现y[k]=x[k]+2*x[k-1]
y[1]=x[1]+2*0=1    (x[1]之前状态都用0)
y[2]=x[2]+2*x[1]=2+2*1=4

(2)y=conv(x,h)是用来实现卷级的,对x序列和h序列进行卷积,输出的结果个数等于x的长度与h的长度之和减去1。

卷积公式:z(n)=x(n)*y(n)= ∫x(m)y(n-m)dm.

程序一:以下两个程序的结果一样

(1)h = [3 2 1 -2 1 0 -4 0 3]; % impulse response

          x = [1 -2 3 -4 3 2 1]; % input sequence

         y = conv(h,x);

         n = 0:14;

         subplot(2,1,1);

         stem(n,y);

         xlabel('Time index n'); ylabel('Amplitude');

        title('Output Obtained by Convolution'); grid;

(2)x1 = [x zeros(1,8)];

          y1 = filter(h,1,x1);

          subplot(2,1,2);

         stem(n,y1);

         xlabel('Time index n'); ylabel('Amplitude');

         title('Output Generated by Filtering'); grid;


程序二:filter和conv的不同

               x=[1,2,3,4,5];
               h=[1,1,1];

               y1=conv(h,x)
               y2=filter(h,1,x)
               y3=filter(x,1,h)

 结果:y1 =1     3     6     9    12     9     5

             y2 = 1     3     6     9    12

‍             y3 =1     3     6  

可见:filter函数y(n)是从n=0开始,认为所有n<0都为0;而conv是从卷积公式计算,包括n<0部分。

                因此filter 和conv 的结果长短不同

程序三:滤波后信号幅度的变化

                num=100; %总共1000个数
                x=rand(1,num); %生成0~1随机数序列 
                x(x>0.5)=1;
                x(x<=0.5)=-1;
                h1=[0.2,0.5,1,0.5,0.2];
                h2=[0,0,1,0,0];
                y1=filter(h1,1,x);
                y2=filter(h2,1,x);
                n=0:99;
                subplot(2,1,1);
                stem(n,y1);
                subplot(2,1,2);
                stem(n,y2);

可见:滤波后信号的幅度是发生变化的,最大幅度值也会变化。

点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 6

    粉丝
  • 0

    好友
  • 15

    获赞
  • 6

    评论
  • 105788

    访问数
关闭

站长推荐 上一条 /3 下一条


手机版| 小黑屋| 关于我们| 联系我们| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2025-9-10 20:40 , Processed in 0.025501 second(s), 7 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部