<jupyter_start><jupyter_text>Generate a sound sample<jupyter_code># frequency is the number of times a wave repeats a second
 
frequency = 1000
num_samples = 48000
 
# The sampling rate of the analog to digital convert
sampling_rate = 48000.0
amplitude = 16000 
file = "test.wav"
sine_wave = [np.sin(2 * np.pi * frequency * x/sampling_rate) for x in range(num_samples)]

plt.plot(sine_wave)
# zoomed in spectrogram
frequencies, times, spectrogram = signal.spectrogram(sine_wave, sample_rate)

# all spectrogram
plt.pcolormesh(times, frequencies, spectrogram)
plt.imshow(spectrogram)
plt.ylabel('Frequency')
plt.gca().invert_yaxis()
plt.xlabel('Time')
plt.show()
nframes=num_samples 
comptype="NONE"
compname="not compressed" 
nchannels=1
sampwidth=2<jupyter_output><empty_output><jupyter_text>Save signal to file<jupyter_code>wav_file = wave.open(os.path.join(data_path, file), 'w')
wav_file.setparams((nchannels, sampwidth, int(sampling_rate), nframes, comptype, compname))

for s in sine_wave:
   wav_file.writeframes(struct.pack('h', int(s*amplitude)))<jupyter_output><empty_output><jupyter_text>Open saved file<jupyter_code>frame_rate = 48000.0
infile = "test.wav"
num_samples = 48000
 
wav_file = wave.open(os.path.join(data_path, file), 'r')
data = wav_file.readframes(num_samples)
wav_file.close()
data = struct.unpack('{n}h'.format(n=num_samples), data)
data = np.array(data)
data_fft = np.fft.fft(data)
# This will give us the frequency we want
frequencies = np.abs(data_fft)
print("The frequency with highest values is {} Hz".format(np.argmax(frequencies)))
plt.subplot(2,1,1)
plt.plot(data[:300])
plt.title("Original audio wave")
plt.subplot(2,1,2)
plt.plot(frequencies)
plt.title("Frequencies found")
plt.xlim(0,1200)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13333333333333333, 'fraction_numerical': 0.02711864406779661, 'mean_word_length': 7.123853211009174, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '321573', 'license': 'Apache-2.0', 'n_tokens_mistral': 730, 'n_tokens_neox': 673, 'n_words': 135, 'path': 'notebooks/exploratory/.ipynb_checkpoints/test_simple_sine-checkpoint.ipynb', 'repo_name': 'MariusDgr/AudioMining', 'size': '1770'}