is it possible to remove thermal noise completely from audiofile by using nlms or rls algorithm?
44 views (last 30 days)
Show older comments
% below is the code I came up with after many different references but some residue thermal noise still remains and % the filtered signal sounds grainy can it be made better or this is it
function adaptive_nlms_audio_filter()
% Adaptive NLMS Audio Noise Filtering with Dynamic Parameter Adjustment
clc;
clear;
close all;
% Get audio input from the user
[audio, fs] = getAudioInput();
% Normalize the audio
audio = audio / max(abs(audio));
% Add Nyquist-Johnson noise (white Gaussian noise)
noise_power = 0.01; % Adjust noise power as needed
noise = sqrt(noise_power) * randn(size(audio)); % Generate noise
noisy_audio = audio + noise;
% Analyze the spectrum of the input audio
[freq, magnitude] = analyzeSpectrum(noisy_audio, fs);
% Adaptive NLMS Parameters
M = determineFilterOrder(freq, magnitude); % Dynamically determine filter order
mu = determineStepSize(magnitude); % Dynamically determine step size
epsilon = 1e-6; % Regularization factor
w = zeros(M, 1); % Filter coefficients
filtered_nlms = zeros(size(noisy_audio));
% Adaptive filtering using NLMS
for i = M:length(noisy_audio)
x = flip(noisy_audio(i-M+1:i)); % Input vector
e = audio(i) - w' * x; % Error signal
w = w + (mu / (epsilon + norm(x)^2)) * x * e; % Update filter coefficients
filtered_nlms(i) = w' * x; % Filter output
end
% Plot the results
figure;
subplot(3, 1, 1);
plot(audio);
title('Original Audio');
xlabel('Samples');
ylabel('Amplitude');
subplot(3, 1, 2);
plot(noisy_audio);
title('Noisy Audio');
xlabel('Samples');
ylabel('Amplitude');
subplot(3, 1, 3);
plot(filtered_nlms);
title('Filtered Audio (Adaptive NLMS)');
xlabel('Samples');
ylabel('Amplitude');
% Buttons for playing audio
uicontrol('Style', 'pushbutton', 'String', 'Play Original', ...
'Position', [20 20 100 30], ...
'Callback', @(~,~) sound(audio, fs));
uicontrol('Style', 'pushbutton', 'String', 'Play Noisy', ...
'Position', [140 20 100 30], ...
'Callback', @(~,~) sound(noisy_audio, fs));
uicontrol('Style', 'pushbutton', 'String', 'Play Filtered', ...
'Position', [260 20 100 30], ...
'Callback', @(~,~) sound(filtered_nlms, fs));
end
function [audio, fs] = getAudioInput()
% Function to get audio input from user
choice = menu('Select Input Method', 'Record Audio', 'Load Audio File');
switch choice
case 1
% Record audio
fs = 44100; % Sampling rate
duration = 5; % Recording duration in seconds
recObj = audiorecorder(fs, 16, 1);
disp('Start speaking...');
recordblocking(recObj, duration);
disp('Recording stopped.');
audio = getaudiodata(recObj);
case 2
% Load audio file
[filename, pathname] = uigetfile({'*.wav', 'WAV Files (*.wav)'}, 'Select an Audio File');
if isequal(filename, 0)
error('No file selected.');
end
[audio, fs] = audioread(fullfile(pathname, filename));
otherwise
error('Invalid choice.');
end
% Normalize the audio
audio = audio / max(abs(audio));
end
function [freq, magnitude] = analyzeSpectrum(signal, fs)
% Analyze the frequency spectrum of the signal
N = length(signal);
freq = (0:N/2-1) * (fs / N);
fft_spectrum = fft(signal);
magnitude = abs(fft_spectrum(1:N/2));
end
function M = determineFilterOrder(freq, magnitude)
% Dynamically determine filter order based on signal spectrum
peak_freq = freq(magnitude == max(magnitude)); % Find the dominant frequency
if peak_freq < 1000
M = 32; % Low-frequency dominant signal
elseif peak_freq < 5000
M = 64; % Mid-frequency dominant signal
else
M = 128; % High-frequency dominant signal
end
end
function mu = determineStepSize(magnitude)
% Dynamically determine step size based on magnitude distribution
energy = sum(magnitude.^2);
if energy < 0.1
mu = 0.01; % Low-energy signal
elseif energy < 1
mu = 0.1; % Medium-energy signal
else
mu = 0.2; % High-energy signal
end
end
1 Comment
Mathieu NOE
on 20 Nov 2024 at 17:02
hello
I am not sure these algorithms are the best choice for audio denoising (without some artifacts including original signal distorsion).
Have you looked at wavelet denoising ?
Answers (0)
See Also
Categories
Find more on Audio I/O and Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!