|
| 1 | +# feature extraction and preprocessing data |
| 2 | +#IMPORT THE LIBRARIES |
| 3 | +import librosa |
| 4 | +import librosa.display |
| 5 | +import pandas as pd |
| 6 | +import numpy as np |
| 7 | +import scipy.signal |
| 8 | +import matplotlib.pyplot as plt |
| 9 | +from PIL import Image |
| 10 | +from pathlib import Path |
| 11 | +from pylab import rcParams |
| 12 | +rcParams['figure.figsize'] = 14, 6 |
| 13 | + |
| 14 | +import csv |
| 15 | +# Preprocessing |
| 16 | +from sklearn.model_selection import train_test_split |
| 17 | +from sklearn.preprocessing import LabelEncoder, StandardScaler |
| 18 | +#Reports |
| 19 | +from sklearn.metrics import classification_report, confusion_matrix |
| 20 | + |
| 21 | +import warnings |
| 22 | +warnings.filterwarnings('ignore') |
| 23 | + |
| 24 | +#READ THE AUDIO SAMPLES |
| 25 | +sr = 16000 |
| 26 | +e_file1 = r'C:\Users\Aryan\PycharmProjects\Noise Reduction Filter\audio01.mp3' |
| 27 | +e_file2 = r'C:\Users\Aryan\PycharmProjects\Noise Reduction Filter\audio02.mp3' |
| 28 | + |
| 29 | +# 10 seconds of each file |
| 30 | +y1,sr = librosa.load(e_file1, mono=True, sr=sr, offset=0, duration=10) |
| 31 | +y2,sr = librosa.load(e_file2, mono=True, sr=sr, offset=0, duration=10) |
| 32 | + |
| 33 | +from IPython.display import Audio, IFrame, display |
| 34 | + |
| 35 | +display(Audio(y1,rate=sr)) |
| 36 | +display(Audio(y2,rate=sr)) |
| 37 | + |
| 38 | +#The audio samples used have high level background noises. |
| 39 | +librosa.display.waveplot(y1,sr=sr, color='g', x_axis='time'); |
| 40 | +librosa.display.waveplot(y1,sr=sr, color='g', x_axis='time'); |
| 41 | + |
| 42 | +#Logmel-spectogram |
| 43 | +#It is a very common preprocessing technique in audio detection applications is to transform audios to its log mel-spectogram representation |
| 44 | + |
| 45 | +S1 = librosa.feature.melspectrogram(y=y1, sr=sr, n_mels=64) |
| 46 | +D1 = librosa.power_to_db(S1, ref=np.max) |
| 47 | +librosa.display.specshow(D1, x_axis='time', y_axis='mel'); |
| 48 | + |
| 49 | +S2 = librosa.feature.melspectrogram(y=y2, sr=sr, n_mels=64) |
| 50 | +D2 = librosa.power_to_db(S2, ref=np.max) |
| 51 | +librosa.display.specshow(D2, x_axis='time', y_axis='mel'); |
| 52 | + |
| 53 | +#Filtering low-frequencies |
| 54 | +#A low-pass filter is a filter that passes signals with a frequency lower than a selected cutoff frequency and attenuates signals with frequencies higher than the cutoff frequency. |
| 55 | +#The exact frequency response of the filter depends on the filter design. |
| 56 | + |
| 57 | +from scipy import signal |
| 58 | +import random |
| 59 | + |
| 60 | + |
| 61 | +def f_high(y,sr): |
| 62 | + b,a = signal.butter(10, 2000/(sr/2), btype='highpass') |
| 63 | + yf = signal.lfilter(b,a,y) |
| 64 | + return yf |
| 65 | + |
| 66 | +yf1 = f_high(y1, sr) |
| 67 | +yf2 = f_high(y2, sr) |
| 68 | + |
| 69 | +librosa.display.waveplot(y1,sr=sr, colour='p', x_axis='time'); |
| 70 | +librosa.display.waveplot(yf1,sr=sr, x_axis='time'); |
| 71 | + |
| 72 | +librosa.display.waveplot(y2,sr=sr, x_axis='time'); |
| 73 | +librosa.display.waveplot(yf2,sr=sr, x_axis='time'); |
| 74 | + |
| 75 | +Sf1 = librosa.feature.melspectrogram(y=yf1, sr=sr, n_mels=64) |
| 76 | +Df1 = librosa.power_to_db(Sf1, ref=np.max) |
| 77 | +librosa.display.specshow(Df1, x_axis='time', y_axis='mel'); |
| 78 | +Sf2 = librosa.feature.melspectrogram(y=yf2, sr=sr, n_mels=64) |
| 79 | +Df2 = librosa.power_to_db(Sf2, ref=np.max) |
| 80 | +librosa.display.specshow(Df2, x_axis='time', y_axis='mel'); |
| 81 | + |
| 82 | +#Check the audio output |
| 83 | +display(Audio(yf1,rate=sr)) |
| 84 | +display(Audio(yf2,rate=sr)) |
| 85 | + |
| 86 | +#CONCLUSION |
| 87 | +#For both audio samples, the filter helped to isolate the interesting frequencies. The first audio is in a very good quality for distincting the birds. |
| 88 | +#The second audio still has some noise but significant improvements in noise reduction can be observed. |
0 commit comments