is it possible to remove thermal noise completely from audiofile by using nlms or rls algorithm?
1 次查看(过去 30 天)
显示 更早的评论
% 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 个评论
Mathieu NOE
2024-11-20
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 ?
回答(1 个)
Saurav
2024-12-24
Hi @Harsh
I understand that you want to completely remove the thermal noise from the audio file using NLMS or RLS filtering algorithms, as implemented in the code above.
It's important to note that completely removing noise is not possible. However, if the filtered signal sounds grainy and some thermal noise remains, you can consider a few adjustments and improvements to potentially enhance the filtering performance
- Adaptive Parameters
- Re-evaluate how you determine the filter order M and step size mu. These parameters heavily influence the filter's performance.
- Consider using a more sophisticated method for parameter adjustment, such as machine learning techniques or more advanced signal processing algorithms.Here's an example code to experiment with dynamic parameter tuning which can be implemented in your code:
% Example of dynamic adjustment within the loop
for i = M:length(noisy_audio)
x = flip(noisy_audio(i-M+1:i)); % Input vector
e = audio(i) - w' * x; % Error signal
if mod(i, 1000) == 0 % Adjust parameters every 1000 samples
M = determineFilterOrder(freq, magnitude);
mu = determineStepSize(magnitude);
end
w = w + (mu / (epsilon + norm(x)^2)) * x * e; % Update filter coefficients
filtered_nlms(i) = w' * x; % Filter output
end
This code snippet adds a dynamic adjustment within the loop, recalculating M and mu periodically. Experiment with different intervals and criteria for adjustment.
2. Noise Power
- Make sure the noise level is appropriate. It may be difficult for the filter to adjust if it is set too high.You can experiment with different levels to find a suitable balance for your application. Additionally, consider reviewing research papers to understand the typical noise power levels used in your specific application.
3. Filter Order
- A longer filter (higher M) can capture more characteristics of the noise but may also introduce more complexity. Test different lengths to see what works best for your signal.
4. Post processing
- Implement additional post-processing techniques, such as smoothing or low-pass filtering, to reduce any residual artifacts in the filtered signal. Refer to the following documentation to learn more about Low-Pass filtering: https://www.mathworks.com/discovery/low-pass-filter.html
5. Wavelet Denoising
- Wavelet denoising can be used as an effective technique to reduce noise in signals, including audio signals. Wavelet denoising works by transforming the signal into the wavelet domain, where it becomes easier to separate noise from the actual signal based on their frequency characteristics. Refer to the following documentation link to learn more: https://www.mathworks.com/help/wavelet/ug/wavelet-denoising.html
Additionally, MATLAB provides built-in functions and toolboxes that support these adaptive filtering techniques:
- adaptfilt Objects: MATLAB provides objects like dsp.LMSFilter, dsp.RLSFilter and dsp.FilteredXLMSFilter which are part of the DSP System Toolbox and are used to implement adaptive filters efficiently. Refer to the following link to learn about the different adaptive filter system objects and their usages: https://www.mathworks.com/help/dsp/ref/dsp.lmsfilter.msesim.html
- DSP System Toolbox: Contains blocks and functions for designing adaptive filters such as LMS, RLS, and Kalman filters.
I hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!