PSD Computing

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/296962602

Power spectral density (PSD) estimation: MATLAB algorithm implementation


for array signal processing and test validation

Code · March 2016


DOI: 10.13140/RG.2.2.31347.02088

CITATIONS READS

2 8,814

2 authors:

Luciano Caldas Luiz Baccala


German Aerospace Center (DLR) University of São Paulo
30 PUBLICATIONS 129 CITATIONS 117 PUBLICATIONS 5,139 CITATIONS

SEE PROFILE SEE PROFILE

All content following this page was uploaded by Luciano Caldas on 24 October 2016.

The user has requested enhancement of the downloaded file.


Power spectral density (PSD) estimation: MATLAB
algorithm implementation for array signal processing
and test validation
Second Version: With experimental data

Luciano C. Caldas and Luiz A. Baccalá


Universidade de São Paulo, Escola Politécnica da USP - Brazil
[email protected]

Introduction

Spectral estimation is an important and useful tool for signal analysis. It, however,
has several practical limitations according to signal characteristics that demand the use of
several different approaches to minimize some of the problems like power leakage, spectra
blurring, bias, etc. [10, 9, 11]
For the current purposes, spectral estimation is used for acoustic signals analysis in
aeroacoustic [8] and beamforming applications [1, 2, 3, 6, 5, 7]. These signals can be seen as
having continuous spectra derived from their nature as regular stochastic processes with
no strictly sinusoidal tones. This is contrast to problems relating the electrical power
network whose signals are comprised of 60 Hz and its odd harmonics.
Our acoustic data acquisition experiments usually consist of 30 sec of acquired data
allowing the comfortable use of the well known Welch spectral estimation method [10],
in which, as is customary in the literature [9], we adopt 50% of overlap between signal
segments before averaging the absolute squared value of segments to obtain the spectral
density estimate. The Hanning window [10, 8] is the most popular one due to its good
trade-off between main-lobe aperture and reduced side-lobe levels.
On applying windows in the case of continuous spectra, careful normalization is
required to ensure that the power computed over time is the same as the one that would
be obtained integrating the spectrum. This was the main motivation of this study: How
to compute correctly the lose of energy caused by the windowing, in terms of signal’s

i
energy conservation. On the other hand, if the spectrum contains pure sinusoids the
normalization that needs to be applied is different from that of a continuous spectrum
leading to some prevailing confusion [4].
The following sections describe the method used for the spectral estimation of signals
with continuous spectra together with a numerical test for code validation. Finally, two
algorithms written in MATLAB are shown: one for cross-spectra computation and a
second one for testing the proposed code.

Formulation used for power spectral density estima-


tion

Let two the data records of two signals x and y of length comprising N samples, be
broken into blocks x(k) and y(k) of size L . In this way, the equivalent block of cross-
(k)
spectrum Ŝxy is estimated by means of
2 · F(x(k) · w) · conj(F(y(k) · w))
Ŝ(k)
xy (f ) = (1)
fs · wi2
P

where w = [w1 . . . wi . . . wL ]T is a suitable data taper window of length L (usually Hanning


window [8]) and fs is the sampling rate (missed out in the first version of this document).
The symbol F{·} stands for the discrete time Fourier transform computed at suitable
frequencies f between 0 and one half the Nyquist frequency. The factor 2 comes from
using a single-sided representation (positive frequencies only) of the spectrum and conj{.}
is the complex-conjugated.
Finally, the cross-spectrum is computed by averaging the terms in
N/L (k)
X Ŝxy
Ŝxy (f ) = (2)
k=1
N/L

Test methodology
Theoretical response

To test implementation compliance we generated a continuous spectrum process by


filtering white noise through a filter with known transfer function. The filter has 10 poles
and zeros. All zeros are placed at the origin and poles are complex conjugate and close
to the unit circumference to emulate high resonances.
Filter characteristics are summed up in the following:
Radius Angle [rad]
0.99 ±π/7
0.98 ±2π/7
0.97 ±3π/7
0.96 ±4π/7
0.95 ±5π/7

ii
and its transfer function H(z) is given by
1
H(z) =
1 − 1.82z −1 + 3.14z −2 − 3.8z −3 + 4.53z −4 − 4.35z −5 + 4.17z −6 − 3.19z −7 + 2.48z −8 − 1.31z −9 + 0.74z −10

1
0.8
0.6
Imaginary Part 0.4
0.2
10
0
-0.2
-0.4
-0.6
-0.8
-1
-1 -0.5 0 0.5 1
Real Part

Figure 1: Poles and zeros map for the filter used.

Results

The results of applying the data generated according to the previously sections can be
appreciated in Figs 2 and 3. Different L values are used to compare window effects on
spectral estimation. To help in the comparison, the theoretical spectral density obtained
using the filter transfer-function (20 × log10 H(f )) is also shown, proving goodness of fit.
Another parameter used to check the results is the signal power. The variance of the
data record (total power) must be equal to the integral of the PSD (Parseval’s theorem),
i.e,
Z +∞
2
σx = Rxx (0) = Sxx (f )df (3)
−∞
which can be computed by the sum of all its terms times the frequency step size, or
L/2−1
fs X
Pxy = · Ŝxy (fi ) (4)
L i=0
where fs is the sampling frequency.
The variance is close to the summed PSD results up-to small numerical computation
differences as seen next:

signal variance: 20.0559


PSD integral w/ L=1024: 20.0762
PSD integral w/ L=512: 20.2197
PSD integral w/ L=256: 20.0352
PSD integral w/ L=128: 20.0537

This good agreement means that the code is working properly.

iii
Power spectra test
30
theoretical spec
Window 1024
Window 512
20 Window 256
Window 128

10
PSD [dB]

-10

-20

-30
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
Normalized frequency

Figure 2: Results for PSD estimated with different window sizes.

Power spectra test


theoretical spec
Window 1024
Window 512
25 Window 256
Window 128

20
PSD [dB]

15

10

0.05 0.1 0.15 0.2 0.25 0.3


Normalized frequency

Figure 3: PSD zoom-in the poles.

Experimental data
Experimental data from NASA-Glenn [6, 7] was used to validate the code and test
the effects of different windows and length. The data was acquired by an array of 90
microphones flush mounted in a duct. An aeroengine fan is placed downstream close to
this array. This setup is largely used for aeroacoustic purpose and the development of
duct-acoustics, beamforming and modal analysis techniques based on microphone signal
processing.

iv
The spectrum shape is expected to have broadband (like a low-pass frequency response
filter) and harmonic tones, called BPF (blade passing frequency). As the fan blades rotate,
the interaction of their periodic wakes with the flow generate pure tones with frequency
given by f = B.Ω, where B is the number of blades in the fan and Ω is the fan speed.
In order to compare the effect of different window length and type, two different results
are portrayed as follows.

Window length effect


The window length has direct relation with the frequency resolution, as the discrete
frequency step is given by: ∆f = fs /L, where L is the window length. On the other hand,
by Eq. 2, the smaller the block is, the more blocks of FFT are averaged out contributing
then to reduce the spectra bias. Again, we face a common trade-off in spectrum analysis
which is the choice of the window length. When the number of samples available is
not great enough, a careful choice must be taken considering spectra bias and frequency
resolution.

105 Kaiser(4096,6)
Kaiser(2048,6)
Kaiser(1024,6)
Kaiser(512,6)
100 Hanning(2048)

95
SPL/ dB

90

85

80

75

70
539 1078 1617 2156 2695 3773 4851
f/ Hz

Figure 4: PSD estimated with different window length. Bellow : Zoom-in the
first tone.

Fig. 4 shows a typical PSD for the noise generated by a turbomachinery. We can see

v
a broadband decaying with frequency and the typical harmonic tones. The bottom plot
shows a closer look to the first tone (or BPF) and the difference in the results is clearly
seen. A bigger window shows a much better capability to represent small tones and tones
appear “narrower” comparing to smaller windows. In fact tones are ideally given by an
impulse function, δ(x), however, due to practical limitations (shuch as sampling frequency,
number of samples, etc) the representation of this function is seen as a “sinc” function
(sin(x)/x). In other words, a bigger window leads to a better approximation of the sinc
function.
This leads to a trivial problem: The estimation of tone amplitudes, as their values
change depending on the window length.

Window type effect


Another concern to give attention is the choice of a suitable data taper. Each option
has its pros and cons against each other [9, 10, 11]. In this example we fixed the window
length in 4096 samples and changed through 5 different window, as seen in Fig. 5.

Kaiser(4096,6)
105 hanning(4096)
blackman(4096)
flattopwin(4096)
100 rectwin(4096)

95
SPL/ dB

90

85

80

75

539 1078 1617 21562695 3773 4851


f/ Hz

Figure 5: PSD estimated with 5 different data taper. Bellow : Zoom-in the first
tone.

Indeed the result obtained with Kaiser (β = 6) and Hanning window is the same. The

vi
green curve (rectangular window, i.e., no window) shows a great capability to find the
tone, however, a huge amount of leakage (highlighted in the plot). On the other hand,
the flat-top (purple) showed great amount of smearing in the tone.
In this example, the Hanning and Kaiser window were the best choice in terms of
spectrum leakage and smearing effect.

Variance
Finally, we can again compare the PSD integration (Eq. 4) and the signal variance (Eq.
3), as previously. It is worth to remember that the data base comprises 90 microphones,
so, now we are able to compare 90 signal variances. This result is shown in Fig. 6. For all
channels, red crosses match with the blue circle, which means that the power spectrum has
no bias in the sense of power conservation and the energy loss caused by the windowing
is correctly compensated.

SPL levels of each mic for file name 20140827_1.wav


129
Variance of each mic# from raw signal
Mean SPL
128 Integration of each PSD

127

126
SPL/ dB

125

124

123

122

121

120
0 10 20 30 40 50 60 70 80 90
microphone index #

Figure 6: Comparison between PSD integration and signal variance.

Conclusions
The quite simple PSD estimation proposed in this work showed good agreement when
compared to a known power spectra. The main motivation for that was compute correctly
the energy loss due to the windowing on the signal segments. As seen, the PSD estimated
preserves the energy of the signal (variance) independently of the data tapper type and
length used.
In this second version, experimental data was added in order to evaluate the effect of
window type and length. Kaiser and Hanning window showed in this experiment a great
choice with respect to power leakage and smearing. Results obtained are in agreement as
observed in literature.
Finally, the MATLAB code attached in annex section allows to compute the cross-
spectra matrix of all cross-spectral combinations with high performance.

vii
Annex

Matlab codes for spectra estimation and validation. It is necessary the ‘Signal Processing’
toolbox.
>>welch estimator.m

%% Function for spectra estimation by Welch’s method


% Developed by Luiz A. Baccala, Fl?vio Caduda and Luciano Caldas, all from
% Escola Polit?cnica - Poli-USP, with cooperation of Carlos Pagani and Felipe
% Amaral from Escola de Engenharia de S?o Carlos - EESC-USP.
%
% Cross-spectra matrix are estimated by Welch’s method with 50% overlap and
% the window energy loss are compasated by a factor of 1/sum(Wi.^2) where
% Wi are the elements of the window [1]. Then, the spectra becomes:
% Sxy = fft(x)*conj(fft(y))/sum(Wi.^2)
%
% Code was tested with a known- spectra signal from a white noise filtered
% by a filter. The variance (power) of the signal checks with the integral
% of the PSD estimated.
%
% INPUT:
% -- signals: matrix of signals to perform the spectra estimatino. Size is
% [Samples x number of sensors];
% -- fs: samplerate in Hertz;
% -- window: data taper desired. Must be a vector. For best performance it
% should be a power of 2. For general applications do: window=hanning(1024);
%
% OUTPUT:
% -- CSM: Cross Spectral Matrix: Unilateral (0:fs/2) spectra. Welch’s
% method is used with 50% overlap. Matrix size: sensors x sensors x
% windowsize/2
% -- frequencies: vector with all frequencies corresponding to each layer
% (3rd layer in depth) of CSM.
%
% LAST REVISION: Aug - 18 - 2016
% ADDED ’fs’ missing term in line 82, for calibration factor
% [1] Trobs,M.; Heinzel,G. "Improved spectrum estimation from digitized
% time series on a logarithmic frequency axis"
% doi:10.1016/j.measurement.2005.10.010

function [CSM,frequencies] = welch_estimator(signals,fs,window)

if size(signals,2) > size(signals,1)


signals = signals.’;
end
window = window(:);
sensors = size(signals,2);
windowsize = length(window);
frequencies = (0:(windowsize/2-1))*fs/windowsize;
block_samples = windowsize; %must be even, best if 2^n

signal_samples = size(signals,1);
number_of_signals = size(signals,2);
back_shift = block_samples./2;
number_of_blocks = floor((2*signal_samples)./block_samples) -1;

data_taper = window;
data_taper = repmat(data_taper,1,number_of_signals);

% Data segmentation into blocks of size block_samples:


S = zeros(block_samples/2,number_of_signals.^2);
for a = 1:number_of_blocks
% Retrieve current data block
Data_Block = signals((a-1)*back_shift+1:block_samples +(a-1)*back_shift,:);
Data_Block = Data_Block - repmat(mean(Data_Block),block_samples,1);
Data_Block = Data_Block.*data_taper; %Taper it

Data_Block = fft(Data_Block); %FFT it,


% bilateral DFT

viii
Data_Block = Data_Block(1:block_samples/2,:);

%All spectral combinations:


P = zeros(block_samples/2,number_of_signals.^2);
c = 1;
for aa = 1:size(Data_Block,2)
for b = aa:size(Data_Block,2)
% P(:,c) = Data_Block(:,b).*conj(Data_Block(:,aa)); % THIS
% IS FOR WIND TUNNEL EESC-USP BEAMFORMING CODE
P(:,c) = Data_Block(:,aa).*conj(Data_Block(:,b)); % THIS
% IS FOR FAN RIG BEAMFORMING CODE
c = c+1;
end
end
% Sum the spectrums up ...
S = S + P;
end

S = S*2/(sum(window.^2)*fs*number_of_blocks); % Average them out


Sf = zeros(sensors,sensors,size(S,1));
c=1;
for a = 1:sensors
for b = a:sensors
Sf(a,b,:) = S(:,c);
c = c+1;
end
end
clear S
CSM = Sf;
for i = 1:size(CSM,3)
CSM(:,:,i) = CSM(:,:,i) + CSM(:,:,i)’ - eye(sensors).*CSM(:,:,i);
end
end

Code for testing


>> test spectra.m

clear all; clc; close all


% model parameters for test
r = 0.99; % Radius of the first pole
kk = 1:5;
rr = r.^kk; % all poles’ radius
thetha = pi/7; % First pole angle
thethar = thetha.*kk; % all poles’ angle
rN = 14;
rM = 4;
Burnin = 10000;
x = randn(1,Burnin+2^(rN+1)); % white noise
% para gerar espectro te?rico do sinal de teste
xa = 1;
for i=1:5
% ’xa’ will have the coefficients for the theoretical spectra
xa = conv([1 -2*rr(i)*cos(thethar(i)) rr(i)^2],xa);
x = filter(1,[1 -2*rr(i)*cos(thethar(i)) rr(i)^2],x); % filter the signal
% to then generate 5 tonal peaks
end
% burn the transient
x = x(Burnin+1:end);
%%
linewidth = 1.3;
x = x(:);
disp([’signal variance: ’ num2str(var(x))])
figure, hold on;
[ft,w]=freqz(1,xa,1024);
plot(w*.5/pi,20*log10(abs(ft)),’r.’,’LineWidth’,linewidth)
wind = [1024 512 256 128];
for i=1:length(wind)
[spec,f]=welch_estimator(x,1,hann(wind(i)));
plot(f,10*log10(abs(reshape(spec/2,length(f),1))),’LineWidth’,linewidth)
disp([’PSD integral w/ L=’ num2str(wind(i)) ’: ’ num2str(sum(spec)*f(2))])

ix
end

legend( ’theoretical spec’,...


[’Window ’ num2str(wind(1))],...
[’Window ’ num2str(wind(2))],...
[’Window ’ num2str(wind(3))],...
[’Window ’ num2str(wind(4))])
xlabel(’Normalized frequency’); ylabel(’PSD [dB]’);
title(’Power spectra test’);
grid
set(gca, ’FontSize’, 13)

References
[1] Caldas, L.C., et al., In-duct Beamforming Noise Source Estimation and Mode Detec-
tion University of São Paulo Fan Rig, 21st AIAA/CEAS Aeroacoustics Conference.
June, 2015.

[2] Caldas, L. C., et al. In-duct Rotating Beamforming and Mode Detection of Fan Noise
Sources. 22nd AIAA/CEAS Aeroacoustics Conference. 2016.

[3] Amaral, F.R., Souza, D.S., Pagani, C.C, Medeiros, M.F. Experimental study of the
effect of a small 2D excrescence placed on the slat cove surface of an airfoil on its
acoustic noise, 21st AIAA/CEAS Aeroacoustics Conference. June, 2015.

[4] Tröbs, M. and Heinzel, G., Improved spectrum estimation from digitized time series
on a logarithmic frequency axis, Measurement 39 (2006) 120-129.

[5] Lowis, Christopher R., In-duct measurement techniques for the characterisation of
broadband aeroengine noise, University of Southampton, 2007.

[6] Dougherty, R.P. and Walker, B.E. , Virtual Rotating Microphone Imaging of Broad-
band Fan Noise, AIAA-2009-3121, 2009.

[7] Sutliff, D.L., A Mode Propagation Database Suitable for Code Validation Utilizing the
NASA Glenn Advanced Noise Control Fan and Artificial Sources, AIAA 2014-0719.

[8] Mueller, T.J., Aeroacoustic Measurements, Springer, 2002 ed.

[9] Percival, B.D. and Walden, A.T., Spectral Analysis for Physical Applications, Cam-
bridge University Press, 1993.

[10] Kay, Steven M. Modern Spectral Estimation: Theory & Application , Prentice-Hall,
1987.

[11] Stoica, P., Moses, Randolph L. Introduction to spectral analysis, Prentice Hall, 1997.

View publication stats