zoukankan      html  css  js  c++  java
  • Matlab 几种卷积的实现与比较(conv与filter,conv2与filter2)

    Matlab 几种卷积的实现与比较(conv与filter,conv2与filter2)

       
    最近在做控制算法实现的时候,对于其中参杂的各种差分、卷积很头疼,就在网上搜集了些资料,汇总于此,以做备忘。

    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=1开始,认为所有n<1都为0;而conv是从卷积公式计算,包括n<1部分。

                    因此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);



    MATLAB中提供了卷积运算的函数命令conv2,其语法格式为: 
    C = conv2(A,B) 
    C = conv2(A,B)返回矩阵A和B的二维卷积C。若A为ma×na的矩阵,B为mb×nb的矩阵,则C的大小为(ma+mb-1)×(na+nb-1)。 


    例: 
    A=magic(5) 
    A = 
    17 24 1 8 15 
    23 5 7 14 16 
    4 6 13 20 22 
    10 12 19 21 3 
    11 18 25 2 9 
    >> B=[1 2 1 ;0 2 0;3 1 3] 
    B = 
    1 2 1 
    0 2 0 
    3 1 3 
    >> C=conv2(A,B) 
    C = 
    17 58 66 34 32 38 15 
    23 85 88 35 67 76 16 
    55 149 117 163 159 135 67 
    79 78 160 161 187 129 51 
    23 82 153 199 205 108 75 
    30 68 135 168 91 84 9 
    33 65 126 85 104 15 27 
    MATLAB图像处理工具箱提供了基于卷积的图象滤波函数filter2,filter2的语法格式为: 
    Y = filter2(h,X) 
    其中Y = filter2(h,X)返回图像X经算子h滤波后的结果,默认返回图像Y与输入图像X大小相同。例如: 
    其实filter2和conv2是等价的。MATLAB在计算filter2时先将卷积核旋转180度,再调用conv2函数进行计算。 
    Fspecial函数用于创建预定义的滤波算子,其语法格式为: 
    h = fspecial(type) 
    h = fspecial(type,parameters) 
    参数type制定算子类型,parameters指定相应的参数,具体格式为: 
    type='average',为均值滤波,参数为n,代表模版尺寸,用向量表示,默认值为[3,3]。 
    type= 'gaussian',为高斯低通滤波器,参数有两个,n表示模版尺寸,默认值为[3,3],sigma表示滤波器的标准差,单位为像素,默认值为0.5

  • 相关阅读:
    JSTL之迭代标签库
    java中的IO流
    浅谈代理模式
    TreeSet集合
    网络编程之UDP协议
    Java中的多线程
    Java中的面向对象
    JavaScript 函数表达式
    JavaScript 数据属性和访问器属性
    JavaScript 正则表达式语法
  • 原文地址:https://www.cnblogs.com/1995hxt/p/4957732.html
Copyright © 2011-2022 走看看