matlab中卷積convolution與filter用法


轉自:https://blog.csdn.net/dkcgx/article/details/46652021

轉自:https://blog.csdn.net/Reborn_Lee/article/details/83279843

 

 

conv(向量卷積運算)
所謂兩個向量卷積,說白了就是多項式乘法。 比如:p=[1 2 3],q=[1 1]是兩個向量,p和q的卷積如下: 把p的元素作為一個多項式的系數,多項式按升冪(或降冪)排列,比如就按升冪吧,寫出對應的多項式:1+2x+3x^2;同樣的,把q的元素也作為多項式的系數按升冪排列,寫出對應的多項式:1+x。
卷積就是“兩個多項式相乘取系數”。 (1+2x+3x^2)×(1+x)=1+3x+5x^2+3x^3 所以p和q卷積的結果就是[1 3 5 3]。
記住,當確定是用升冪或是降冪排列后,下面也都要按這個方式排列,否則結果是不對的。 你也可以用matlab試試 p=[1 2 3] q=[1 1] conv(p,q) 看看和計算的結果是否相同。
conv2(二維矩陣卷積運算)
a=[1 1 1;1 1 1;1 1 1]; b=[1 1 1;1 1 1;1 1 1]; >> conv2(a,b)
ans =
     1     2     3     2     1  

     2     4     6     4     2    

     3     6     9     6     3    

     2     4     6     4     2  

     1     2     3     2     1
>> conv2(a,b,'valid')
ans =
     9
>> conv2(a,b,'same')
ans =

4     6     4  

6     9     6

4     6     4
>> conv2(a,b,'full')
ans =

1     2     3     2     1       

2     4     6     4     2      

3     6     9     6     3      

2     4     6     4     2      

1     2     3     2     1

convn(n維矩陣卷積運算)

 

>> a=ones(5,5,5)
a(:,:,1) =

1     1     1     1     1      

1     1     1     1     1      

1     1     1     1     1      

1     1     1     1     1      

1     1     1     1     1
a(:,:,2) =
     1     1     1     1     1      1     1     1     1     1      1     1     1     1     1      1     1     1     1     1      1     1     1     1     1
a(:,:,3) =
     1     1     1     1     1      1     1     1     1     1      1     1     1     1     1      1     1     1     1     1      1     1     1     1     1
a(:,:,4) =
     1     1     1     1     1      1     1     1     1     1      1     1     1     1     1      1     1     1     1     1      1     1     1     1     1
a(:,:,5) =
     1     1     1     1     1      1     1     1     1     1      1     1     1     1     1      1     1     1     1     1      1     1     1     1     1
>> b=ones(5,5,5);
>> convn(a,b,'valid')
ans =
   125
>> convn(a,b,'same')
ans(:,:,1) =
    27    36    45    36    27

    36    48    60    48    36

    45    60    75    60    45

    36    48    60    48    36

    27    36    45    36    27
ans(:,:,2) =
    36    48    60    48    36     48    64    80    64    48     60    80   100    80    60     48    64    80    64    48     36    48    60    48    36
ans(:,:,3) =
    45    60    75    60    45     60    80   100    80    60     75   100   125   100    75     60    80   100    80    60     45    60    75    60    45
ans(:,:,4) =
    36    48    60    48    36     48    64    80    64    48     60    80   100    80    60     48    64    80    64    48     36    48    60    48    36
ans(:,:,5) =
    27    36    45    36    27     36    48    60    48    36     45    60    75    60    45     36    48    60    48    36     27    36    45    36    27
>> convn(a,b)
ans(:,:,1) =
     1     2     3     4     5     4     3     2     1

     2     4     6     8    10     8     6     4     2

     3     6     9    12    15    12     9     6     3

     4     8    12    16    20    16    12     8     4

     5    10    15    20    25    20    15    10     5

     4     8    12    16    20    16    12     8     4

     3     6     9    12    15    12     9     6     3

     2     4     6     8    10     8     6     4     2

     1     2     3     4     5     4     3     2     1
ans(:,:,2) =
     2     4     6     8    10     8     6     4     2      4     8    12    16    20    16    12     8     4      6    12    18    24    30    24    18    12     6      8    16    24    32    40    32    24    16     8     10    20    30    40    50    40    30    20    10      8    16    24    32    40    32    24    16     8      6    12    18    24    30    24    18    12     6      4     8    12    16    20    16    12     8     4      2     4     6     8    10     8     6     4     2
ans(:,:,3) =
     3     6     9    12    15    12     9     6     3      6    12    18    24    30    24    18    12     6      9    18    27    36    45    36    27    18     9     12    24    36    48    60    48    36    24    12     15    30    45    60    75    60    45    30    15     12    24    36    48    60    48    36    24    12      9    18    27    36    45    36    27    18     9      6    12    18    24    30    24    18    12     6      3     6     9    12    15    12     9     6     3
ans(:,:,4) =
     4     8    12    16    20    16    12     8     4      8    16    24    32    40    32    24    16     8     12    24    36    48    60    48    36    24    12     16    32    48    64    80    64    48    32    16     20    40    60    80   100    80    60    40    20     16    32    48    64    80    64    48    32    16     12    24    36    48    60    48    36    24    12      8    16    24    32    40    32    24    16     8      4     8    12    16    20    16    12     8     4
ans(:,:,5) =
     5    10    15    20    25    20    15    10     5     10    20    30    40    50    40    30    20    10     15    30    45    60    75    60    45    30    15     20    40    60    80   100    80    60    40    20     25    50    75   100   125   100    75    50    25     20    40    60    80   100    80    60    40    20     15    30    45    60    75    60    45    30    15     10    20    30    40    50    40    30    20    10      5    10    15    20    25    20    15    10     5
ans(:,:,6) =
     4     8    12    16    20    16    12     8     4      8    16    24    32    40    32    24    16     8     12    24    36    48    60    48    36    24    12     16    32    48    64    80    64    48    32    16     20    40    60    80   100    80    60    40    20     16    32    48    64    80    64    48    32    16     12    24    36    48    60    48    36    24    12      8    16    24    32    40    32    24    16     8      4     8    12    16    20    16    12     8     4
ans(:,:,7) =
     3     6     9    12    15    12     9     6     3      6    12    18    24    30    24    18    12     6      9    18    27    36    45    36    27    18     9     12    24    36    48    60    48    36    24    12     15    30    45    60    75    60    45    30    15     12    24    36    48    60    48    36    24    12      9    18    27    36    45    36    27    18     9      6    12    18    24    30    24    18    12     6      3     6     9    12    15    12     9     6     3
ans(:,:,8) =
     2     4     6     8    10     8     6     4     2      4     8    12    16    20    16    12     8     4      6    12    18    24    30    24    18    12     6      8    16    24    32    40    32    24    16     8     10    20    30    40    50    40    30    20    10      8    16    24    32    40    32    24    16     8      6    12    18    24    30    24    18    12     6      4     8    12    16    20    16    12     8     4      2     4     6     8    10     8     6     4     2
ans(:,:,9) =
     1     2     3     4     5     4     3     2     1      2     4     6     8    10     8     6     4     2      3     6     9    12    15    12     9     6     3      4     8    12    16    20    16    12     8     4      5    10    15    20    25    20    15    10     5      4     8    12    16    20    16    12     8     4      3     6     9    12    15    12     9     6     3      2     4     6     8    10     8     6     4     2      1     2     3     4     5     4     3     2     1

 

conv

Convolution and polynomial multiplication

Syntax

w = conv(u,v)

w = conv(u,v,shape)

Description

w = conv(u,v)返回向量u和v的卷積。如果u和v是多項式系數的向量,則對它們進行卷積相當於將兩個多項式相乘。

 

w = conv(u,v,shape) returns a subsection of the convolution, as specified by shape. For example, conv(u,v,'same') returns only the central part of the convolution, the same size as u, and conv(u,v,'valid') returns only the part of the convolution computed without the zero-padded edges.

w = conv(u,v,shape)返回卷積的子部分,由形狀指定。 例如,conv(u,v,'same')僅返回卷積的中心部分,與u的大小相同,而conv(u,v,'valid')僅返回計算后的卷積部分而沒有零填充邊。


Polynomial Multiplication via Convolution

 

Create vectors u and v containing the coefficients of the polynomials x^2 + 1 and 2x + 7.

u = [1 0 1];
v = [2 7];

Use convolution to multiply the polynomials.

w = conv(u,v)
w = 1×4

     2     7     2     7

w contains the polynomial coefficients for  2x^3 + 7x^2 + 2x + 7.


Vector Convolution

Create two vectors and convolve them.

u = [1 1 1];
v = [1 1 0 0 0 1 1];
w = conv(u,v)
w = 1×9

     1     2     2     1     0     1     2     2     1

The length of w is length(u)+length(v)-1, which in this example is 9.


Central Part of Convolution

Create two vectors. Find the central part of the convolution of u and v that is the same size as u.

u = [-1 2 3 -2 0 1 2];
v = [2 4 -1 1];
w = conv(u,v,'same')
w = 1×7

    15     5    -9     7     6     7    -1

w has a length of 7. The full convolution would be of length length(u)+length(v)-1, which in this example would be 10.



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM