forked from Pim-Mostert/decoding-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
decode_beamformer.m
54 lines (46 loc) · 2.04 KB
/
decode_beamformer.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
function Xhat = decode_beamformer(cfg0, decoder, Y)
% [Xhat] = decode_beamformer(cfg, decoder, Y)
% Estimate the activity of latent components using a linear decoder, obtained from an
% appropriate training function. Several components may be estimated independently.
%
% decoder The linear decoder obtained from e.g. train_beamformer.
% Y Matrix of size F x N, where F is the number of features and the N the number of trials,
% that contains the data that is to be decoded.
% cfg Configuration struct that can possess the following fields:
% .demean Whether the data should be demeaned (per feature,
% over trials) prior to decoding. The mean can be
% specified in the following ways:
% = 'trainData' The mean of the training data (default).
% = 'testData' The mean of the testing data.
% = [F x 1] vector Manually specified mean, where F is the number of
% features (e.g. sensors).
% = 'no' No demeaning.
%
% Xhat Vector or matrix of size C x N, where C is the number of components, containing
% the decoded data.
%
% See also TRAIN_BEAMFORMER.
% Created by Pim Mostert, 2016
if ~isfield(cfg0, 'demean')
cfg0.demean = 'trainData';
end
% Convert to matrix
numN = size(Y, 2);
% Demean
if strcmp(cfg0.demean, 'trainData')
if ~isfield(decoder, 'mY')
error('No mean found in decoder');
end
Y = Y - repmat(decoder.mY, [1, numN]);
elseif strcmp(cfg0.demean, 'testData')
m = nanmean(Y, 2);
Y = Y - repmat(m, [1, numN]);
elseif isnumeric(cfg0.demean)
Y = Y - repmat(cfg0.demean, [1, numN]);
elseif strcmp(cfg0.demean, 'no')
else
error('Demeaning configuration ''%s'' is unknown', cfg0.demean);
end
% Decode
Xhat = decoder.W*Y;
end