Traitement du signal avec Python - Labs 5 et 6
Вставка
- Опубліковано 15 вер 2024
- Code pour lab 5 :
import IPython.display as ipd
import requests
import io
import wave
import matplotlib.pyplot as plt
import numpy as np
import random
from scipy.io import wavfile
from google.colab import drive
drive.mount('/content/drive')
folderName = "/content/drive/My Drive/"
NbSamples = 512
URL to the .wav audio file
audio_url = "www2.cs.uic.ed..."
Fetch the audio file
response = requests.get(audio_url)
audio_content = response.content
Create an in-memory file-like object for the audio content
audio_io = io.BytesIO(audio_content)
Open and read the audio file using the wave module
with wave.open(audio_io, 'rb') as wav_file:
audio_data = wav_file.readframes(-1)
sample_rate = wav_file.getframerate()
Convert the audio data to a numpy array
audio_array = np.frombuffer(audio_data, dtype=np.int16)
Plot the first NbSamples (512) samples of the audio
plt.figure(figsize=(10, 4))
plt.plot(audio_array[:NbSamples])
plt.title("First 512 Samples of Audio")
plt.xlabel("Sample Index")
plt.ylabel("Amplitude")
plt.grid()
plt.show()
ampl = 0.008*max(audio_array)/len(audio_array)
noise = ampl*np.asarray(random.sample(range(0,len(audio_array)),len(audio_array)))
noisy_arrayFloat = audio_array + noise
noisy_array = noisy_arrayFloat.astype(np.int16)
plt.figure(figsize=(10, 4))
plt.plot(noisy_array[:NbSamples])
plt.title("Noisy Signal")
plt.xlabel("Sample Index")
plt.ylabel("Amplitude")
plt.grid()
plt.show()
print("type of audio_array: ", audio_array.dtype)
print("type of noisy_arrayFloat: ", noisy_arrayFloat.dtype)
print("type of noisy_array: ", noisy_array.dtype)
filename = folderName + "noisy.wav"
wavfile.write(filename, sample_rate, noisy_array)
ipd.display(ipd.Audio(filename, rate=sample_rate))
Code pour lab 6 :
import IPython.display as ipd
import requests
import io
import wave
import matplotlib.pyplot as plt
import numpy as np
import ipywidgets as widgets
from IPython.display import display, clear_output
URL to the .wav audio file
audio_url = "www2.cs.uic.ed..."
Fetch the audio file
response = requests.get(audio_url)
audio_content = response.content
Create an in-memory file-like object for the audio content
audio_io = io.BytesIO(audio_content)
Open and read the audio file using the wave module
with wave.open(audio_io, 'rb') as wav_file:
audio_data = wav_file.readframes(-1)
sample_rate = wav_file.getframerate()
Convert the audio data to a numpy array
audio_array = np.frombuffer(audio_data, dtype=np.int16)
Number of samples to display
NbSamples = 256
Initial sample index
sample_index = - NbSamples
Function to display the next set of samples
def display_next_samples(b):
global sample_index
clear_output(wait=True) # Clear the previous plot
sample_index += NbSamples
plt.figure(figsize=(10, 4))
plt.plot(audio_array[sample_index:sample_index+NbSamples])
plt.title(f"Samples {sample_index} to {sample_index+NbSamples-1}")
plt.xlabel("Sample Index")
plt.ylabel("Amplitude")
plt.grid()
plt.show()
display_buttons()
Function to display the previous set of samples
def display_previous_samples(b):
global sample_index
clear_output(wait=True) # Clear the previous plot
sample_index = max(0, sample_index - NbSamples)
plt.figure(figsize=(10, 4))
plt.plot(audio_array[sample_index:sample_index+NbSamples])
plt.title(f"Samples {sample_index} to {sample_index+NbSamples-1}")
plt.xlabel("Sample Index")
plt.ylabel("Amplitude")
plt.grid()
plt.show()
display_buttons()
Create button styles
next_button_style = widgets.ButtonStyle(
button_color='orange',
font_style='italic',
font_weight='bold',
font_variant="small-caps",
text_color='black',
text_decoration='underline'
)
previous_button_style = widgets.ButtonStyle(
button_color='yellow',
font_style='italic',
font_weight='bold',
font_variant="small-caps",
text_color='white',
text_decoration='underline'
)
Create buttons with custom styling
next_button = widgets.Button(description="Next", style=next_button_style)
previous_button = widgets.Button(description="Previous", style=previous_button_style)
Link the buttons to their respective functions
next_button.on_click(display_next_samples)
previous_button.on_click(display_previous_samples)
Function to display buttons
def display_buttons():
display(widgets.HBox([previous_button, next_button]))
Display the buttons initially
display_buttons()
Play the audio using IPython's display.Audio
ipd.display(ipd.Audio(audio_url, rate=sample_rate))