You are on page 1of 3
# Name- Somnath # ID No- 2023H1240062H import numpy as np import matplotlib.pyplot as plt import soundfile as sf from scipy.signal import spectrogram from IPython.display import Audio, display # Load audio file file path = "/content/gostopaudio.wav" audio_signal, fs = sf.read(file_path) # Convert stereo to mono if needed if audio_signal.ndim == 2: audio_signal = np.mean(audio_signal, axis=1) # Add Gaussian noise to the audio signal noise level = 0.1 noise = noise level * np.random.randn(len(audio_signal)) noisy audio signal = audio signal + noise # RLMS filter parameters delta = le-5 # Small positive constant to avoid singularity order = 32 # Filter order lambda_param = 0.99 # Forgetting factor (adjusted) # RLMS filter function def rlms filter(input_signal, desired signal, order, delta, ‘lambda param) : ‘num samples = len(input signal) weights = np.zeros (order) P = delta * np.eye(order) output signal = np.zeros(num samples) mse_values = np.zeros(num_samples - order) for i in range(order, num_samples) : x = input_signal{i-order:i] y_hat = np.dot(weights, x) error = desired signal{i] - y hat P = (1 / lambda_param) * (P --np.outer(P @ x, P @ x) / (lambda_param + x @ P @ x) weights = weights + P @ x * error output_signal[i] = y_hat mse_values[i - order] = np.mean((desired_signal[i-order+1:i+1] > y_hat) **°2) return output_signal, mse_values # Apply RLMS filter to the noisy audio signal desired_signal = audio_signal # Using the original audio as the desired signal for denoising filtered_audio, mse_values = rlms_filter(noisy audio signal, desired Signal, order, delta, lambda_param) # Calculate the error between the input audio and the filtered signal error = audio signal - filtered_audio # Play the Original Audio display (Audio(audio_signal, rate=ts)) # Play the Noisy Audio display (Audio(noisy audio signal, rate=fs)) # Play the Filtered Audio display (Audio( filtered audio, rate=fs)) # Plotting plt.figure(figsize=(12, 16)) plt.subplot(7, 1, 1) plt.plot(audio_signal, label='Original Audio Signal’) plt.title( ‘Original Audio Signal") plt.xlabel(‘Sample") plt.ylabel( ‘Amplitude’ ) plt.legend() plt.subplot(7, 1, 2) plt.plot(noisy audio signal, label="Noisy Audio Signal', alpha=0.7) plt.title(*Noisy Audio Signal') plt.xlabel('Sample") plt.ylabel ( ‘Amplitude’ ) plt.legend() plt.subplot(7, 1, 3) plt.plot(filtered audio, label='Filtered Audio Signal (RLMS)', Linestyle=" dashed’ ) plt.title( ‘Filtered Audio Signal (RLMS)') plt.xlabel('Sample") plt.ylabel (‘Amplitude’) plt.legend() plt.subplot(7, 1, 4) plt.plot(error, label='Error', color='red') plt.title(‘Error Between Original and Filtered Signals’) plt.xlabel( 'Sample") plt.ylabel( ‘Amplitude’ ) plt.legend() plt.subplot(7, 1, 5) plt.plot(mse values, label='Time Domain MSE‘, color='green') plt.title(*Time Domain MSE') plt.xlabel( 'Sample*) plt.ylabel( 'MSE') plt.legend() plt. tight_layout () plt.show() Original Aus Signal {: c- - ° ‘50600 = 150000 200000 7 eT ses | bey Teena 7 S = eee

You might also like