Last active
December 22, 2023 10:10
-
-
Save djbr1/6ddf97872850b5f76cafe7cbd1b5ba47 to your computer and use it in GitHub Desktop.
band limited noise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# | |
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) | |
# | |
# | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
spectral bandwidth of generated mono wav file can be examined using soundspec