用signal.convolve函数实现x(n)和h(n)的卷积输出,其中x(n)=(2n+1)R_N (n), h(n)=(n-3)[u(n+3)-u(n-7)]并绘图
时间: 2023-05-29 13:01:23 浏览: 93
首先,导入必要的库和工具,并定义x(n)和h(n)的函数:
```python
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
def x(n):
N = 10
return 2*n>=0 and 2*n<N and 1 or 0
def h(n):
return (n-3)*(n>=3 and n<7)
```
然后,生成x(n)和h(n)的序列:
```python
N = 10
n = np.arange(N)
x_seq = np.array([x(i) for i in n])
h_seq = np.array([h(i) for i in n])
```
接着,调用 `signal.convolve` 函数,计算卷积输出:
```python
y_seq = signal.convolve(x_seq, h_seq, 'full')
```
最后,绘制 x(n), h(n),以及卷积输出 y(n) 的图像:
```python
fig, axs = plt.subplots(3, 1, figsize=(6, 8))
axs[0].stem(n, x_seq, linefmt='C0-', markerfmt='C0o', basefmt='k-')
axs[0].set_title('x(n)=(2n+1)R_N(n)')
axs[1].stem(n, h_seq, linefmt='C1-', markerfmt='C1o', basefmt='k-')
axs[1].set_title('h(n)=(n-3)[u(n-3)-u(n-7)]')
l = len(y_seq)
axs[2].stem(np.arange(l), y_seq, linefmt='C2-', markerfmt='C2o', basefmt='k-')
axs[2].set_title('Output y(n)')
plt.tight_layout()
plt.show()
```
输出结果:
![image.png](attachment:image.png)
阅读全文