Hi! can someone help me with this code? I want filter a noise in a wav file using moving average filters

1 view (last 30 days)
This what I have done so far
clear all;
clc;
%%=== Reading the file ===
[signal, Fs] = audioread('testSignal.wav');
subplot(211)
plot(signal(1:350));
title('Original Signal')
xlabel('Samples')
ylabel('Amplitude')
grid on;
sound(signal)
% ========================
%%=== Filter ===
order =10000;
h = ones(1,order)/order;
count = 0;
for n = 1:length(signal)-(order-1)
for j = n:n+(order-1)
count = count+1;
x(count) = signal(j);
end
count = 0;
FiltSig(n) = sum(h.*x);
end
% ==============
%%=== Writing the file ===
audiowrite('FiltSignal.wav',FiltSig,Fs)
subplot(212)
plot(FiltSig(1:350));
title('Filtered Signal')
xlabel('Samples')
ylabel('Amplitude')
grid on;
sound(FiltSig)
% ========================

Answers (1)

Image Analyst
Image Analyst on 7 Apr 2016
Instead of doing the moving average yourself, simply use conv() or lowess().
windowWidth = 100; % Whatever.
kernel = ones(1, windowWidth)/windowWidth;
smoothedSignal = conv(signal, kernel, 'same');
  2 Comments
Image Analyst
Image Analyst on 8 Apr 2016
I don't have that toolbox so you'll just have to read the help. conv() is simply the sum of the products, though you can weight them if you want. You can also use sgolayfilt() which is a Savitzky-Golay filter in the Signal Processing Toolbox. That is a sliding filter too but it fits the data in a sliding window to a polynomial, like a line, quadratic, cubic, or whatever. You might try that if you want. I think there are others like butter() and filtfilt() and filter() but I don't use any of those. I only use conv() and sgolayfilt().

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!