BAMQBackEnd.m 5.97 KB
Newer Older
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
function binQ = BAMQBackEnd(mITDref,  mIVSref, mILDref, mPowRef1,  mPowRef2, ...
                   mITDtest, mIVStest, mILDtest, mPowTest1, mPowTest2, fc, fsNew)

%% Initialization
ivsThres = 0.98;

% parameters for equation 14 & 15
k.r = 2;
k.s = 0.0012;
k.t = 666;
k.a = 0.023;

n.r = 3.17;
n.s = -0.0015;
n.t = 560;
n.a = -2.75;

% initialize block computation
blockLenFac = 0.4;
blockLen = round(blockLenFac.*fsNew);
nShifts = floor(size(mITDref,1)/blockLen);
nResiSamples = mod(size(mITDref,1),blockLen);
if nResiSamples ~= 0,
    nShifts = nShifts + 1;
end

vILDdiffs = [];
vITDdiffs = [];
vIVSdiffs = [];

%% Computation

for iShift = 1:nShifts,
    % find frequency bands with most power
    if iShift == nShifts,
        vPowShiftRef1 = mean(mPowRef1( (iShift-1)*blockLen+1:end,:));
        vPowShiftTest1 = mean(mPowTest1((iShift-1)*blockLen+1:end,:));
        vPowShiftRef2 = mean(mPowRef2( (iShift-1)*blockLen+1:end,:));
        vPowShiftTest2 = mean(mPowTest2((iShift-1)*blockLen+1:end,:));
    else
        vPowShiftRef1 = mean(mPowRef1( (iShift-1)*blockLen+1:(iShift-1)*blockLen+blockLen,:));
        vPowShiftTest1 = mean(mPowTest1((iShift-1)*blockLen+1:(iShift-1)*blockLen+blockLen,:));
        vPowShiftRef2 = mean(mPowRef2( (iShift-1)*blockLen+1:(iShift-1)*blockLen+blockLen,:));
        vPowShiftTest2 = mean(mPowTest2((iShift-1)*blockLen+1:(iShift-1)*blockLen+blockLen,:));
    end
    vPowShiftRef1 = real(10.*log10(vPowShiftRef1));
    vPowShiftTest1 = real(10.*log10(vPowShiftTest1));
    vPowShiftRef2 = real(10.*log10(vPowShiftRef2));
    vPowShiftTest2 = real(10.*log10(vPowShiftTest2));
    
    maxPowRef = max(max(vPowShiftRef1,vPowShiftRef2));
    maxPowTest = max(max(vPowShiftTest1,vPowShiftTest2));
    idxRef1 = find(vPowShiftRef1 > maxPowRef-10);
    idxRef2 = find(vPowShiftRef2 > maxPowRef-10);
    idxTest1 = find(vPowShiftTest1 > maxPowTest-10);
    idxTest2 = find(vPowShiftTest2 > maxPowTest-10);
    idxUnion = union([idxRef1 idxRef2],[idxTest1 idxTest2]);

    for jBand = idxUnion,

        % load IVS
        idxBandDiff = abs(idxUnion-jBand);
        idxNeighborBands = find(idxBandDiff == 1 | idxBandDiff == 0); %
        if iShift == nShifts,
            vIVSbandRef  = mean(mIVSref( (iShift-1)*blockLen+1:end,idxUnion(idxNeighborBands)),2);
            vIVSbandTest = mean(mIVStest((iShift-1)*blockLen+1:end,idxUnion(idxNeighborBands)),2);
        else
            vIVSbandRef  = mean(mIVSref( (iShift-1)*blockLen+1:(iShift-1)*blockLen+blockLen,idxUnion(idxNeighborBands)),2);
            vIVSbandTest = mean(mIVStest((iShift-1)*blockLen+1:(iShift-1)*blockLen+blockLen,idxUnion(idxNeighborBands)),2);
        end

        % ILD sub-measure, equation 6
        % -------------------------------------------------------------------------

        if iShift == nShifts,
            mILDbandRef  = mILDref( (iShift-1)*blockLen+1:end,jBand);
            mILDbandTest = mILDtest((iShift-1)*blockLen+1:end,jBand);
        else
            mILDbandRef  = mILDref( (iShift-1)*blockLen+1:(iShift-1)*blockLen+blockLen,jBand);
            mILDbandTest = mILDtest((iShift-1)*blockLen+1:(iShift-1)*blockLen+blockLen,jBand);
        end
        
        mILDbandRef(mILDbandRef > 10) = 10;
        mILDbandRef(mILDbandRef < -10) = -10;
        mILDbandTest(mILDbandTest > 10) = 10;
        mILDbandTest(mILDbandTest < -10) = -10;
        ILDsumPlusRef  = sum(abs(mILDbandRef(mILDbandRef > 0)));
        ILDsumMinusRef = sum(abs(mILDbandRef(mILDbandRef <  0)));
        ILDsumPlusTest  = sum(abs(mILDbandTest(mILDbandTest > 0)));
        ILDsumMinusTest = sum(abs(mILDbandTest(mILDbandTest <  0)));
        ILDdiff = abs(ILDsumPlusRef - ILDsumPlusTest) + abs(ILDsumMinusRef - ILDsumMinusTest);

        % ITD sub-measure, equation 7-9
        % -------------------------------------------------------------------------
        if iShift == nShifts,
            vITDbandRef  = mITDref( (iShift-1)*blockLen+1:end,jBand);
            vITDbandTest = mITDtest((iShift-1)*blockLen+1:end,jBand);
        else
            vITDbandRef  = mITDref( (iShift-1)*blockLen+1:(iShift-1)*blockLen+blockLen,jBand);
            vITDbandTest = mITDtest((iShift-1)*blockLen+1:(iShift-1)*blockLen+blockLen,jBand);
        end

        vITDbandRef(abs(vITDbandRef) > 700e-6) = NaN;
        idxHighIVSref = vIVSbandRef > ivsThres & [diff(vIVSbandRef)>=0 ; zeros(1,1)];
        vITDbandRef(~idxHighIVSref) = NaN;

        vITDbandTest(abs(vITDbandTest) > 700e-6) = NaN;
        idxHighIVStest = vIVSbandTest > ivsThres & [diff(vIVSbandTest)>=0 ; zeros(1,1)];
        vITDbandTest(~idxHighIVStest) = NaN;

        ITDsumPlusRef  = nansum(abs(vITDbandRef(vITDbandRef >= 0)))./(blockLen-sum(isnan(vITDbandRef))+eps);
        ITDsumMinusRef = nansum(abs(vITDbandRef(vITDbandRef <  0)))./(blockLen-sum(isnan(vITDbandRef))+eps);
        ITDsumPlusTest  = nansum(abs(vITDbandTest(vITDbandTest >= 0)))./(blockLen-sum(isnan(vITDbandTest))+eps);
        ITDsumMinusTest = nansum(abs(vITDbandTest(vITDbandTest <  0)))./(blockLen-sum(isnan(vITDbandTest))+eps);
        ITDdiff = abs(ITDsumPlusRef - ITDsumPlusTest) + abs(ITDsumMinusRef - ITDsumMinusTest);

        % IVS sub-measure, equation 12-15
        % -------------------------------------------------------------------------
        K = k.r./(1+exp(k.s.*(fc(jBand)-k.t))) + k.a;
        N = n.r./(1+exp(n.s.*(fc(jBand)-n.t))) + n.a;

        dIVSref = exp(K+N) - exp(K.*vIVSbandRef+N);
        dIVStest = exp(K+N) - exp(K.*vIVSbandTest+N);

        IVSdiff = abs(mean(dIVSref)-mean(dIVStest));
        % -------------------------------------------------------------------------

        if iShift == nShifts && nResiSamples > 0,
            ILDdiff = ILDdiff.*(blockLen/nResiSamples);
            ITDdiff = ITDdiff.*(blockLen/nResiSamples);
        end
               
        vILDdiffs = [vILDdiffs ILDdiff];
        vITDdiffs = [vITDdiffs ITDdiff];
        vIVSdiffs = [vIVSdiffs IVSdiff];
    end
end

ILDdiff = mean(vILDdiffs);
ITDdiff = mean(vITDdiffs);
IVSdiff = mean(vIVSdiffs);

load ARESmodel

binQ = arespredict(stBinModel, [ILDdiff ITDdiff IVSdiff]);