Simple Signal Smoothing via Signal Averaging using Matlab

Signal averaging is aimed to smooth signals which contain noise. The simple averaging process implemented here is using the idea of adding / summing several sequence of signals which each of them contains random noise. Next, the result of summation process is then divided by number of signal sequence added which then produced an averaged signal which has lower noise level than any of the original single signal. The implementation of the simple signal averaging is carried out using Matlab.

The complete code of signal averaging is shown below added with brief explanations

% taking average of sinusoidal signal with randomized noise
% Jonny Latuny, Feb. 21, 2012

close all; % close all open windows
clear all; % clear all previous / existing variables in workspace

dt = 1/1000; % define dt value
t = dt:dt:200*dt; % devine array of t
sine = sin(2*pi*100*t); % generate a sinusoidal signal based on t value

signal_all = []; % provide empty container to store summation result

for i = 1:10 % perform 10 loops in which 10 parts of signal are produced
y_noisy = sine + randn(size(t)); % signal + randonm noise
eval(['signal_' num2str(i) ' = y_noisy;']); % using eval to generate sequential signal segment
signal_all = [signal_all; eval(['signal_' num2str(i);])]; % concatenate all 10 signal into 1 matrix
end

signal_avr = sum(signal_all) / i; % calculating the average value

FT = fft(y_noisy,512); % FFT of a single signal
P = FT.* conj(FT)/512; % provide FFT result for plotting

FT_avr = fft(signal_avr,512); % FFT of averaged signal
P_avr = FT_avr.* conj(FT_avr)/512; % provide FFT result for plotting

subplot(2,1,1) % subplot 1
plot(P), axis([0 250 0 30]);
title('Spectrum of Single Signal (10th)');
xlabel('data points');
ylabel('magnitude');
grid;

subplot(2,1,2) % subplot 2
plot(P_avr,'r-'), axis([0 250 0 30]);
title('Spectrum of Averaged Signal');
xlabel('data points');
ylabel('magnitude');
grid;

Plot of Signal Averaging Example

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The above composite plot shows the results of the simple signal averaging.

The top subplot shows FFT result of a single signal which contains noise.

The bottom subplot shows FFT result of the averaged signal which calculated using 10 sequences of signal samples as implemented in Matlab codes above.

Generating Daubechies wavelet function (psi) and wavelet scaling (phi) using Matlab

Matlab provides a function to generate wavelet function values and wavelet scaling values via build-in function called wavefun. (type help wavefun at Matlab command windows for information regarding wavefun function).

However, apart from Matlab build-in function,I’ve found out another way to produce psi and phi function during investigation of Wavelet application to mechanical vibration signal. The script invented make use of a function called daub.m which is taken from Uvi Wave v3 (a free wavelet toolbox for Matlab). Uvi Wave v3 can be downloaded from http://cas.ensmp.fr/~chaplais/UviWave/Uvi_Wave_300.zip. After extracting the zip file, daub.m can be copied from Uvi_Wave_300 folder at /Uvi_Wave_300/wfilter/. Copy the daub.m to a folder which has been included in Matlab search path, or include the entire Uvi_wave_300 folder in Matlab search path.

Here are the codes for producing db(x) wavelet function (psi).

clc; clear all; close all;
%
db_type = 4; % db(x), x = 2,4,6,8, ...
[hh,gg,rh,rg] = daub(db_type); % from Uvi Wave 300
h = rh .* 1.414;
g = rg .* 1.414;
%
psi=1;
psi=conv(psi,g);
n=10;
psi=upsample(psi,2);
%
for i=1:n
psi=conv(psi,h);
if i<n
psi=upsample(psi,2);
end
end
%
x=linspace (0,3,length(psi));
%
plot(x,psi)
judul= ['db',num2str(db_type),' psi (wavelet) function'];
title(judul);

And here are the codes for calculating db(x) scaling function (phi),

clc; clear all; close all;
%
db_type = 4; % db(x), x = 2,4,6,8, ...
[h,g,rh,rg] = daub(db_type); % from Uvi Wave 300
h = rh .* 1.414;
%
phi=1; %initial pulse]
phi=conv(phi,h);
n=10; %number of iteration
phi=upsample(phi,2);
%
for i = 1:n
phi = conv(h,phi) ;
if i<n % last convolved need not go for upsampling
phi = upsample(phi,2);
end
end
%
x = linspace(0,3,length(phi));
%
plot(x,phi,'r-')
judul = ['db',num2str(db_type),' phi (scaling) function'];
title(judul);

Below are the plots produced using wavelet psi and phi script presented above.

db4 psi

 

 

 

 

 

 

 

 

 

 

 

 

 

 

db4 phi

 

 

 

 

 

 

 

 

 

 

 

 

 

 

References: