Python使用scipy模組實現一維卷積運算示例
阿新 • • 發佈:2020-01-09
本文例項講述了Python使用scipy模組實現一維卷積運算。分享給大家供大家參考,具體如下:
一 介紹
signal模組包含大量濾波函式、B樣條插值演算法等等。下面的程式碼演示了一維訊號的卷積運算。
二 程式碼
import numpy as np import scipy.signal x = np.array([1,2,3]) h = np.array([4,5,6]) print(scipy.signal.convolve(x,h))#一維卷積運算
三 執行結果
[ 4 13 28 27 18]
四 一維卷積演算法
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<map> #include<algorithm> usingnamespace std; #define INF 0xfffffff #define maxn 100010 int main() { int m=5,n=5; int a[5]={0,1,1},b[5]={0,1}; int i,j; int k=m+n-1;//卷積後陣列長度 int c[k]; memset(c,sizeof(c));//注意一定要清零 /**卷積計算**/ for(i=0; i<k; i++) { for(j=max(0,i+1-n); j<=min(i,m-1); j++) c[i]+=a[j]*b[i-j]; cout<<c[i]<<" "; } /****/ cout<<endl; }
更多關於Python相關內容感興趣的讀者可檢視本站專題:《Python數學運算技巧總結》、《Python資料結構與演算法教程》、《Python函式使用技巧總結》、《Python字串操作技巧彙總》及《Python入門與進階經典教程》
希望本文所述對大家Python程式設計有所幫助。