1. 程式人生 > >Matlab 幾種卷積的實現與比較(conv與filter,conv2與filter2,imfilter)

Matlab 幾種卷積的實現與比較(conv與filter,conv2與filter2,imfilter)

</pre><pre name="code" class="cpp">
</pre></h1><h1><span style="font-size:18px;">1 filter</span></h1><span style="font-size:14px">y=filter(p,d,x)用來實現差分方程,d表示差分方程輸出y的係數,p表示輸入x的係數,而x表示輸入序列。輸出結果長度數等於x的長度。</span><p></p><p></p><p><span style="color:rgb(85,85,85)">實現差分方程,先從簡單的說起:</span><span style="color:rgb(85,85,85)"></span><span style="color:rgb(85,85,85)">filter([1,2],1,[1,2,3,4,5]),實現y[k]=x[k]+2*x[k-1]</span><span style="color:rgb(85,85,85)"></span><span style="color:rgb(85,85,85)">y[1]=x[1]+2*0=1    (x[1]之前狀態都用0)</span><span style="color:rgb(85,85,85)"></span><span style="color:rgb(85,85,85)">y[2]=x[2]+2*x[1]=2+2*1=4</span></p><p><span style="color:rgb(85,85,85)">以此類推......</span></p><h1><span style="color: rgb(85, 85, 85);"><span style="font-size:18px;">2 conv</span></span></h1><p><span style="color: rgb(85, 85, 85);">y=conv(x,h)</span><span style="color:rgb(85,85,85)">是用來實現卷級的,對x序列和h序列進行卷積,輸出的結果個數等於x的長度與h的長度之和減去1。</span></p><p><span style="color:rgb(85,85,85)">卷積公式:z(n)=x(n)*y(n)= ∫x(m)y(n-m)dm.</span></p><h2><span style="color:rgb(85,85,85)"> <span style="font-size:18px;">2.1 </span></span><span style="color: rgb(85, 85, 85);"><span style="font-size:18px;">Filter與conv相同點</span></span></h2><p>h = [3 2 1 -2 1 0 -4 0 3]; % impulse response</p><pre name="code" class="cpp">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;
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