<jupyter_start><jupyter_text>Import of the data raw data<jupyter_code>audio_file="/content/file_example_WAV_2MG.wav"
samples, sample_rate = librosa.load(audio_file, sr=None)
import librosa.display
import matplotlib.pyplot as plt

# x-axis has been converted to time using our sample rate. 
# matplotlib plt.plot(y), would output the same figure, but with sample 
# number on the x-axis instead of seconds
plt.figure(figsize=(14, 5))
librosa.display.waveplot(samples, sr=sample_rate)<jupyter_output><empty_output><jupyter_text>Each second has 16000(depends on the sample rate) sample numbers. the total sample equals to the sample rate*lenghth(seconds) of the audio<jupyter_code>print ('Example shape ', samples.shape, 'Sample rate ', sample_rate, 'Data type', type(samples))
print (samples[22400:22420])<jupyter_output>Example shape  (92160,) Sample rate  48000 Data type <class 'numpy.ndarray'>
[0.03384399 0.03393555 0.03414917 0.03427124 0.03442383 0.03457642
 0.0345459  0.03442383 0.03430176 0.03411865 0.03408813 0.03433228
 0.03445435 0.03439331 0.03436279 0.03430176 0.03396606 0.03375244
 0.03378296 0.03375244]<jupyter_text>Convert to Spectrograms<jupyter_code>sgram = librosa.stft(samples)
librosa.display.specshow(sgram)<jupyter_output>/home/bernoulli/.local/lib/python3.8/site-packages/librosa/display.py:954: UserWarning: Trying to display complex-valued input. Showing magnitude instead.
  warnings.warn(<jupyter_text>Unfortunately, when we display this spectrogram there isn’t much information for us to see.This happens because of the way humans perceive sound.We hear them on a logarithmic scale rather than a linear scale.   Mel Spectrograms<jupyter_code># use the mel-scale instead of raw frequency
sgram_mag, _ = librosa.magphase(sgram)
mel_scale_sgram = librosa.feature.melspectrogram(S=sgram_mag, sr=sample_rate)
librosa.display.specshow(mel_scale_sgram)<jupyter_output><empty_output><jupyter_text>This is better than before, but most of the spectrogram is still dark and not carrying enough useful information. So let’s modify it to use the Decibel Scale instead of Amplitude.<jupyter_code># use the decibel scale to get the final Mel Spectrogram
mel_sgram = librosa.amplitude_to_db(mel_scale_sgram, ref=np.min)
librosa.display.specshow(mel_sgram, sr=sample_rate, x_axis='time', y_axis='mel')
plt.colorbar(format='%+2.0f dB')<jupyter_output><empty_output><jupyter_text>Fine tune the mel spectrogram<jupyter_code>#Spectrogram is a 2D numpy array
print(type(mel_sgram), mel_sgram.shape)<jupyter_output><class 'numpy.ndarray'> (128, 181)<jupyter_text>MFCC (for Human Speech) These essentially take Mel Spectrograms and apply a couple of further processing steps. This selects a compressed representation of the frequency bands from the Mel Spectrogram that correspond to the most common frequencies at which humans speak.<jupyter_code>import sklearn
import librosa
import librosa.display

# Load the audio file
samples, sample_rate = librosa.load(audio_file, sr=None)
mfcc = librosa.feature.mfcc(samples, sr=sample_rate)

# Center MFCC coefficient dimensions to the mean and unit variance
mfcc = sklearn.preprocessing.scale(mfcc, axis=1)
librosa.display.specshow(mfcc, sr=sample_rate, x_axis='time')

print (f'MFCC is of type {type(mfcc)} with shape {mfcc.shape}')<jupyter_output>/home/bernoulli/.local/lib/python3.8/site-packages/sklearn/preprocessing/_data.py:235: UserWarning: Numerical issues were encountered when centering the data and might not be solved. Dataset may contain too large values. You may need to prescale your features.
  warnings.warn(
/home/bernoulli/.local/lib/python3.8/site-packages/sklearn/preprocessing/_data.py:254: UserWarning: Numerical issues were encountered when scaling the data and might not be solved. The standard deviation of the data is probably very close to 0. 
  warnings.warn(<jupyter_text>Data augmentation  Date preprocession method 2 with tensorflow  import package  New Section Aaugment the number of the dataset of the spectrogram (get more input data)<jupyter_code>!pip3 install SpecAugment
!pip install tensorflow-io
!pip install pydub
from pydub import AudioSegment
  
# Open the stereo audio file as
# an AudioSegment instance
stereo_audio = AudioSegment.from_file(
    "/content/file_example_WAV_2MG.wav",
    format="wav")
  
# Calling the split_to_mono method
# on the stereo audio file
mono_audios = stereo_audio.split_to_mono()

# Exporting/Saving the two mono
# audio files present at index 0(left)
# and index 1(right) of list returned
# by split_to_mono method
mono_left = mono_audios[0].export(
    "/content/mono_left.wav",
    format="wav")
mono_right = mono_audios[1].export(
    "/content/mono_right.wav",
    format="wav")
import tensorflow as tf
import tensorflow_io as tfio
audio = tfio.audio.AudioIOTensor('/content/file_example_WAV_2MG.wav')
print(audio)<jupyter_output><AudioIOTensor: shape=[92160     2], dtype=<dtype: 'int16'>, rate=48000><jupyter_text>I replace the fonction tf.squeeze by a slicing technique<jupyter_code>audio_slice = audio[100:]

# remove last dimension
# this is the code before change "audio_tensor = tf.squeeze(audio_slice, axis=-1)"

#the code after is :
audio_tensor = audio_slice[:,0]

print(audio_tensor)
#just reshape the tensor numpy ndarray

audio_tensor = tf.reshape(audio_tensor,(audio_tensor.shape[0],1))
import matplotlib.pyplot as plt


tensor = tf.cast(audio_tensor, tf.float32) / 32768.0

plt.figure()
plt.plot(tensor.numpy())
# Freq masking
freq_mask = tfio.audio.freq_mask(mfcc, param=1)

plt.figure()
plt.imshow(freq_mask.numpy())
# Time masking
time_mask = tfio.audio.time_mask(mfcc, param=20)

plt.figure()
plt.imshow(time_mask.numpy())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09363492898474488, 'fraction_numerical': 0.047869542346133616, 'mean_word_length': 6.112219451371571, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '441110', 'license': 'MIT', 'n_tokens_mistral': 2132, 'n_tokens_neox': 1908, 'n_words': 531, 'path': 'Copy of Voice_data_preprocessing.ipynb', 'repo_name': 'DimitriMenoubia/Translator-Tech', 'size': '5703'}