-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdistortion.m
55 lines (51 loc) · 1.44 KB
/
distortion.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
%calculates distortion as described in 'Optimierung frequencvarianter
%Nullbeamformer für akustische Signale mittels Statistik höherer Ordnung -
%Anwendung im KFZ und in Büroräumen'
ü
%D = distortion(O, P, B)
%O and P are the original and the processed signals in time domain, respectively. B is the blocksize (Default = 128).
function Dist = distortion(O,P,B)
if(nargin<3)
B = 128;
end
if(nargin<2|nargin>3)
error('usage: D = distortion(O,P[,B])');
end
if((~isvector(O))|(~isvector(P)))
error('first two arguments must be vectors');
end
if(length(O)<=B|length(P)<=B)
error('signals must be longer than block length');
end
if(size(O,2)==1)
O = O.';
end
if(size(P,2)==1)
P = P.';
end
%init
if(length(O)<length(P))
sigLength = length(O);
else
sigLength = length(P);
end
blockNum = floor(sigLength / B);
OFFT = zeros(B,blockNum);
PFFT = zeros(size(OFFT));
timeShift = ceil(B/2);
% normalize to standard deviation
ONorm = O/norm(O);
PNorm = P/norm(P);
% short time fft
for blockCnt=1:blockNum
blockIndex = (blockCnt-1)*timeShift +1;
block = ONorm(:,blockIndex:blockIndex+B-1);
block = block .* hann(B,'periodic')';
OFFT(:,blockCnt) = abs(fft(block)).^2;
block = PNorm(:,blockIndex:blockIndex+B-1);
block = block .* hann(B,'periodic')';
PFFT(:,blockCnt) = abs(fft(block)).^2;
end
% compare averaged spectra
Dist = mean(10*log10(mean(PFFT,2)./mean(OFFT,2)));
end