Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to generate rectangular impulse with numpy/scipy

I want to generate a rectangular impulse with python. I believe it can be done with numpy or scipy. But I cannot get it from API. After I generate the rectangular impulse, i will plot it with matplotlib.

like image 512
user2262504 Avatar asked Oct 31 '25 00:10

user2262504


1 Answers

To create a 1D array, all zeros but for a stretch of 1.0 values - a rectangular impulse:

import numpy as np
a = np.zeros( (1000,) )   # whatever size. initializes to zeros
a[150:180] = 1.0          # index range sets location, width of impulse

To see a plot:

import matplotlib.pyplot as mp
mp.plot(a)
mp.show()
like image 119
DarenW Avatar answered Nov 02 '25 13:11

DarenW