stream.close() p.terminate() """ plt.figure() f, psd = signal.welch(transmit, Fs, nperseg=1024) plt.plot(f, 20 * np.log10(psd)) plt.savefig("transmitted_spectrum", dpi=300) filename = 'myAudioFile.wav' # save result to file samples = transmit / max(abs(transmit)) samples = (.5 * transmit + .5) * (2**14) samples = samples.astype(np.int16) wf = wave.open(filename, 'wb') wf.setnchannels(1) wf.setsampwidth( 2) # 2 bytes per sample int16. If unsure, use np.dtype(np.int16).itemsize wf.setframerate(Fs) wf.writeframes(b''.join(samples)) wf.close() with open("transmit.txt", 'w') as fout: for value in transmit: fout.write(str(value) + '\n') audio.play(transmit, volume, Fs) plt.show()
import numpy as np import audio_functions as audio from config import * white_noise = (np.clip(np.random.normal(0.0, 0.2, 44100 * 7), -1.0, 1.0)).astype(np.float32) input("Press enter to play white noise") audio.play(white_noise, volume, Fs) print('End.')
import audio_functions import numpy as np volume = 1.0 fs = 44100 duration = 5.0 sigma = 0.2 # standard deviation of Gaussian (square root of variance) Nsamples = int(duration * fs) gaussian = (np.clip(np.random.normal(0.0, sigma, Nsamples), -1.0, 1.0)).astype(np.float32) #generate gaussian white noise audio_functions.play(gaussian, volume, fs)
import audio_functions as audio import numpy as np interval = 1.0 # interval between impulses (in seconds) pulsewidth = 10 # width of a pulse (in samples) # (you can try width 1 but that's extremely low power...) # samples preparation impulse = pulsewidth * [1] impulse.extend((int(44100 * interval) - pulsewidth) * [0]) impulses = 8 * impulse # now convert to a numpy array with the correct data type impulses = np.array(impulses, dtype=np.float32) # play sample audio.play(impulses, 1.0, 44100)