-
Notifications
You must be signed in to change notification settings - Fork 49
/
octaveBandMean.m
75 lines (63 loc) · 2.49 KB
/
octaveBandMean.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function [ magSpect_oct, freqVec_oct ] = octaveBandMean( magSpect, freqVec, octSpace, centerFreq )
% Given a magnitude spectrum this function will calculate the average (single, third, nth) octave band magnitude
%
% Syntax: [ MAGSPECT_OCT, FREQVEC_OCT ] = OCTAVEBANDMEAN( MAGSPECT, FREQVEC, OCTSPACE, CENTERFREQ )
%
% Inputs:
% magSpect - An arbitrary magnitude spectrum as a vector
% freqVec - The corresponding frequencies for each magnitude value
% octSpace - A single value between 0 and 1 specifying the octave spacing
% centerFreq - The center frequency for the octave band
%
% Outputs:
% magSpect_oct - The magnitude spectrum averaged per octave band
% freqVec_oct - The corresponding frequencies for the output magnitude vector
%
% See also:
% Author: Jacob Donley
% University of Wollongong
% Email: [email protected]
% Copyright: Jacob Donley 2016, 2017
% Date: 8 July 2016
% Revision: 0.2 (29 March 2017)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin < 4
centerFreq=1e3;
end
if nargin < 3
octSpace = 1/3;
end
if size(magSpect,2) > size(magSpect,1), magSpect = magSpect.'; end
if size(freqVec,2) > size(freqVec,1), freqVec = freqVec.'; end
freqVec_ = nonzeros(freqVec); %remove zero values
n = floor(log(freqVec_( 1 )/centerFreq)/log(2)/octSpace): ...
floor(log(freqVec_(end)/centerFreq)/log(2)/octSpace);
freqVec_oct = centerFreq * (2 .^ (n*octSpace));
if freqVec_oct(end) ~= freqVec_(end)
% This may happen if there is no octave band that is centered at the
% Nyquist frequency (fs/2)
freqVec_oct(end+1) = freqVec_(end); % So we find an average at the
% Nyquist frequency that is of the same width as the other octave bands
warning(['The sampling frequency used does not have a Nyquist ' ...
'frequency that, after division by the centre frequency, is a ' ...
'power of two. ' ...
'i.e. (' num2str(freqVec_(end)*2) '/2)/' num2str(centerFreq) ' is ' ...
'not a power of two.']);
end
fd = 2^(octSpace/2);
fupper = freqVec_oct * fd;
flower = freqVec_oct / fd;
octBands = [flower' fupper'];
[~, oBandInds] = min(abs( ...
repmat(freqVec,1,2,length(octBands)) ...
- repmat(permute(octBands,[3 2 1]),length(freqVec),1,1) ));
oBandInds = permute(oBandInds, [3 2 1]);
magSpect_oct = zeros(1,length(freqVec_oct));
for band = 1:length(freqVec_oct)
magSpect_oct(band+1) = mean( ...
magSpect( oBandInds(band,1):oBandInds(band,2) ) );
end
magSpect_oct(1) = magSpect(1);
freqVec_oct = [0 freqVec_oct];
end