track_audio.m 2.81 KB
Newer Older
Fredrik Bagge Carlsson's avatar
tr back  
Fredrik Bagge Carlsson committed
1
2
3
4
5
6
7
8
9
10
11
12
13
clear;
close all;
clc;

fs = 24414;
chunks = 2048;
dt = chunks/fs;

[Bf, Af] = butter(3,8000/fs); % filter vectors


%% Setup objects
% Initialize localization models using braodband and subband settings
Fredrik Bagge Carlsson's avatar
tr back    
Fredrik Bagge Carlsson committed
14

Martin Karlsson's avatar
Martin Karlsson committed
15
% Window size in seconds
Fredrik Bagge Carlsson's avatar
tr back    
Fredrik Bagge Carlsson committed
16
winSec = 20E-3;
Martin Karlsson's avatar
Martin Karlsson committed
17

Fredrik Bagge Carlsson's avatar
tr back    
Fredrik Bagge Carlsson committed
18
19
20
% Lowest and highest center frequency in Hertz of the gammatone filterbank
fLowHz  = 80;
fHighHz = 8000;
Fredrik Bagge Carlsson's avatar
tr back  
Fredrik Bagge Carlsson committed
21
22
23
24
25
26
27
dObj   = dataObject([],fs,10,2);

% Settings for subband approach
par_sub = genParStruct('cc_bBroadband',0,'cc_wSizeSec',winSec,...
    'cc_hSizeSec',winSec/2,'cc_maxDelaySec',1.25e-3,...
    'fb_lowFreqHz',fLowHz,'fb_highFreqHz',fHighHz,...
    'fb_nERBs',1,'ihc_method','none',...
Fredrik Bagge Carlsson's avatar
tr back    
Fredrik Bagge Carlsson committed
28
    'loc_NSources',1);
Fredrik Bagge Carlsson's avatar
tr back  
Fredrik Bagge Carlsson committed
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

% Initialize localization models using braodband and subband settings
mObj  = manager(dObj,'localization',par_sub);

%% Model parameters
sigma_w = 1;

Q = [2/4*dt^4, 1/2*dt^3; 1/2*dt^3, dt^2]*sigma_w;   % Process noise covariance
R = 1;                                              % Measurement noise covariance
x = [0; 0];                                         % Initial state
P = [10, 0; 0, 10];                                 % Initial state covariance

A = [1, dt; 0, 1];                                  % System matrix
c = [1; 0];                                         % Output vector

% Check definiteness of covariance matrices
if ~all(eig(Q) > 0) || ~all(eig(R) > 0) || ~all(eig(A) > 0)
    error('All covariance matrices have to be positive definite.');
end

%% Initialization


% Add necessary paths
addpath('./tools');
addpath('./ekfukf-toolbox');



figure(1)

Fredrik Bagge Carlsson's avatar
tr back    
Fredrik Bagge Carlsson committed
60
N = 100; % The number of steps to run this stuff.
Fredrik Bagge Carlsson's avatar
tr back  
Fredrik Bagge Carlsson committed
61
62
63
64

% Initialize posterior mean and covariance
posteriorMean = zeros(size(A, 1), N);
posteriorCovariance = zeros(size(A, 1), size(A, 1), N);
Martin Karlsson's avatar
Martin Karlsson committed
65
measuredLocations = zeros(N,1);
Fredrik Bagge Carlsson's avatar
tr back  
Fredrik Bagge Carlsson committed
66
67
68
69

% =======================================================
% Main loop - Perform localization and tracking
% =======================================================
Fredrik Bagge Carlsson's avatar
tr back    
Fredrik Bagge Carlsson committed
70
display('Entering main loop')
Fredrik Bagge Carlsson's avatar
tr back  
Fredrik Bagge Carlsson committed
71
72
tic();
t_old = toc();
Martin Karlsson's avatar
Martin Karlsson committed
73

Fredrik Bagge Carlsson's avatar
tr back  
Fredrik Bagge Carlsson committed
74
for l = 1:N
Martin Karlsson's avatar
Martin Karlsson committed
75
    audio = get_audio(2048);
Fredrik Bagge Carlsson's avatar
tr back  
Fredrik Bagge Carlsson committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    t_new = toc();
    dti = t_new - t_old();
    % Request processing
    mObj.processSignal(audio);
    azimEst = dObj.localization{1}.Data(end,1); % There might be an issue with several sources here!
    % Perform Kalman filter prediction and update, TODO: consider changing this
    % crappy filter for a PF
    
    
    Qi = [1/4*dti^4+1e-6, 1/2*dti^3; 1/2*dti^3, dti^2]*sigma_w;   % Process noise covariance
    Ai = [1, dti; 0, 1];
    [x, P] = kf_predict(x, P, Ai, Qi);
    [x, P] = kf_update(x, P, azimEst, c', R);
    
    posteriorMean(:, l) = x;
    posteriorCovariance(:, :, l) = P;
Martin Karlsson's avatar
Martin Karlsson committed
92
93
94
    measuredLocations(l) = azimEst;
    %     pause(max(,0))
    pause(0.01)
Fredrik Bagge Carlsson's avatar
tr back  
Fredrik Bagge Carlsson committed
95
96
97
    t_old = t_new;
end

Martin Karlsson's avatar
Martin Karlsson committed
98
plot(measuredLocations, 'x', 'LineWidth', 2);
Fredrik Bagge Carlsson's avatar
tr back  
Fredrik Bagge Carlsson committed
99
100
101
xlabel('Time / s');
ylabel('Azimuth / deg');
grid on; hold on;
Martin Karlsson's avatar
Martin Karlsson committed
102
plot(posteriorMean(1, :), 'g', 'LineWidth', 2);
Fredrik Bagge Carlsson's avatar
tr back  
Fredrik Bagge Carlsson committed
103
104