Question

I need to generate a Dirac delta signal (all zeroes, except for one sample). How can I achieve this?

I have not found the wave module useful.

Was it helpful?

Solution

This is pretty straightforward using wave, actually.

import wave

data = [128 for i in range(100)] # zeroes
data[50] = 255 # except for one spike
data = bytes(data) # convert to bytes

with open(r'D:\foo.wav', 'wb') as file:
    f = wave.open(file)
    f.setnchannels(1) # mono
    f.setsampwidth(1) 
    f.setframerate(44100) # standard sample rate
    f.writeframes(data)

Here's what the resulting .wav file looks like in Audacity: image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top