Skip to content

Instantly share code, notes, and snippets.

@djbr1
Last active December 22, 2023 10:10
Show Gist options
  • Save djbr1/6ddf97872850b5f76cafe7cbd1b5ba47 to your computer and use it in GitHub Desktop.
Save djbr1/6ddf97872850b5f76cafe7cbd1b5ba47 to your computer and use it in GitHub Desktop.
band limited noise
#
#
def fftnoise(f):
f = np.array(f, dtype='complex')
Np = (len(f) - 1) // 2
phases = np.random.rand(Np) * 2 * np.pi
phases = np.cos(phases) + 1j * np.sin(phases)
f[1:Np+1] *= phases
f[-1:-1-Np:-1] = np.conj(f[1:Np+1])
return np.fft.ifft(f).real
def band_limited_noise(min_freq, max_freq, samples=1024, samplerate=1):
freqs = np.abs(np.fft.fftfreq(samples, 1/samplerate))
f = np.zeros(samples)
idx = np.where(np.logical_and(freqs>=min_freq, freqs<=max_freq))[0]
f[idx] = 1
return fftnoise(f)
from scipy.io import wavfile
# parameters: lower freq limit, upper freq limit, number of samples =samplerate*duration(seconds), sample rate)
x = band_limited_noise(200, 3000, 4410000, 44100)
x = np.int16(x * (2**15 - 1))
wavfile.write("test.wav", 44100, x)
#
#
@djbr1
Copy link
Author

djbr1 commented Dec 22, 2023

spectral bandwidth of generated mono wav file can be examined using soundspec

Screenshot 2023-12-22 at 11 07 43

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment