I am working to convert this MATLAB code that generates a waveform to Python. For context this is a simulation of band excitation response from an atomic force microscope (not r
You can rewrite your Python code using broadcasting
import matplotlib.pyplot as plt
from numpy import (abs, arange, exp, linspace, logical_and, pi,
sign, sin, where)
from numpy.fft import fft, fftshift, ifft, ifftshift
rate = 2*2E6
npx = 128
nptpx = 8192
ω_low, ω_hi = 200000, 2*200000
ω, step = linspace(-rate/2, rate/2, nptpx,
endpoint=0, retstep=1)
p = arange(npx)/npx
A = sin(6*pi*p)+2
ω0 = 250000+100000*p
Q = 100-50*p
φ = sign(p-1/2)
D = where(logical_and(abs(ω)>ω_low, abs(ω)<ω_hi), 1, 0)
R = (D * (A * ω0**2 * exp(1j*φ))[:,None] /
(ω**2 + 1j*ω*(ω0/Q)[:,None] - (ω0**2)[:,None]))
r = ifft(fftshift(R, axes=1))[:,::-1].real.reshape(npx*nptpx)
plt.figure(figsize=(12,4), constrained_layout=1)
plt.plot(r, lw=.4)
plt.xlim(0,len(r)-1)
plt.show()
Compare with your plot