user2611973
user2611973

Reputation: 21

Noisy Scipy Wavfile Output Issue

I'm trying to create a .wav file using scipy.io.wavfile.write(), but the result is a noisy wav file, I have tryed with iNotebook with the same result, here is the code:

import numpy as np
from scipy.io import wavfile

x= np.arange(25600.0)
sig= np.sin(2*np.pi*(1250.0/10000.0)*x) 

def makewav(data,outfile,samplerate):
    wavfile.write(outfile,samplerate,data)

makewav(sig,'foo.wav',44100)

It happens only when I try to generate pure tones. Any problem when reading the .wav whith scipy.io.wavfile.read() and writing again with scipy.io.wavfile.write().

Upvotes: 2

Views: 1633

Answers (1)

nio
nio

Reputation: 5289

According to this documentation you should output integer values, and you have floating point data so you should have to convert them first.

The method:
    scipy.io.wavfile.write(filename, rate, data)[source]

Argument:
    data is a ndarray A 1-D or 2-D numpy array of integer data-type.

To actually convert your data, try to use this code:

data=np.int16(sig/np.max(np.abs(sig)) * 32767)

Check out more in this answer.

Upvotes: 3

Related Questions