Skip to content

Commit

Permalink
Merge pull request #194 from stephaniebrandl/hackathon
Browse files Browse the repository at this point in the history
bug fixed in proc_spectrogram
  • Loading branch information
svendaehne authored Jan 19, 2017
2 parents 5577ffb + 5e42cb7 commit ae77edf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 48 deletions.
91 changes: 45 additions & 46 deletions processing/proc_spectrogram.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@
% dat = proc_spectrogram(dat, freq, <OPT>)
% dat = proc_spectrogram(dat, freq, Window, <OPT>)
% dat = proc_spectrogram(dat, freq, Window, NOverlap,<OPT>)
%
%
%Arguments:
% DAT - data structure of continuous or epoched data
% FREQ - vector with desired frequency bins (eg 1:100). If a single
% integer is given, it specifies the FFT length from which
% the frequency bins are then automatically chosen (this is
% FREQ - vector with desired frequency bins (eg 1:100). If a single
% integer is given, it specifies the FFT length from which
% the frequency bins are then automatically chosen (this is
% usally faster).
% OPT - struct or property/value list of optional properties:
% 'Window' - if a vector, divides the data into segments of length equal
% 'Window' - if a vector, divides the data into segments of length equal
% to the length of WINDOW, and then windows each
% segment with the vector specified in WINDOW. If WINDOW is an integer,
% the data is divided into segments of length equal to that integer value, and a
% Hamming window of equal length is used. If WINDOW is not specified, the
% default is used. Default dat.fs/2 (ie a 500 ms window).
% 'NOverlap' - number of samples each segment of X overlaps. Must be an
% integer smaller than the window length. Default:
% window length -1 (so that a time x frequency
% 'NOverlap' - number of samples each segment of X overlaps. Must be an
% integer smaller than the window length. Default:
% window length -1 (so that a time x frequency
% representation is defined for each sample)
% 'CLab' - specifies for which channels the spectrogram is calculated.
% (default '*')
% 'Output' - Determines if and how the FFT coefficients are processed.
% 'complex' preserves the complex output (with both phase and
% amplitude information), 'amplitude' returns the absolute
% value, 'power' the squared absolute value, 'db' log power,
% and 'phase' the phase in radians. Default 'complex'.
% value, 'power' the squared absolute value, 'db' log power,
% and 'phase' the phase in radians. Default 'complex'.
%Returns:
% DAT - updated data structure with a higher dimension.
% For continuous data, the dimensions correspond to
% For continuous data, the dimensions correspond to
% time x frequency x channels. For epoched data, time x
% frequency x channels x epochs.
% The coefficients are complex. You can obtain the amplitude by
Expand All @@ -48,30 +48,30 @@
% 2010, 2015

props = {'Window', [] 'DOUBLE';
'DbScaled' 1 '!BOOL';
'NOverlap' [] 'DOUBLE[1]';
'CLab', '*' 'CHAR|CELL{CHAR}|DOUBLE[-]';
'Output' 'complex' '!CHAR(complex amplitude power db phase)';
};
'DbScaled' 1 '!BOOL';
'NOverlap' [] 'DOUBLE[1]';
'CLab', '*' 'CHAR|CELL{CHAR}|DOUBLE[-]';
'Output' 'complex' '!CHAR(complex amplitude power db phase)';
};

if nargin==0,
dat= props; return
dat= props; return
end

misc_checkType(dat,'!STRUCT(x fs)');
misc_checkType(freq,'!DOUBLE[-]');

% Parse other arguments
if nargin>2 && isnumeric(varargin{1})
window = varargin{1};
remidx = 1;
if nargin>3 && isnumeric(varargin{2})
noverlap = varargin{2};
remidx =[remidx 2];
varargin = {'Window' varargin{1} 'NOverlap' varargin{2:end}};
else
varargin = {'Window' varargin{:}};
end
window = varargin{1};
remidx = 1;
if nargin>3 && isnumeric(varargin{2})
noverlap = varargin{2};
remidx =[remidx 2];
varargin = {'Window' varargin{1} 'NOverlap' varargin{2:end}};
else
varargin = {'Window' varargin{:}};
end
end

opt= opt_proplistToStruct(varargin{:});
Expand All @@ -89,9 +89,9 @@
dat = misc_history(dat);

if numel(freq)==1
nfreq = freq;
nfreq = freq;
else
nfreq = numel(freq);
nfreq = numel(freq);
end

%% Spectrogram
Expand All @@ -102,12 +102,12 @@
% Process spectrogram channel- and epoch-wise
X=dat.x;
dat.x=[];
if ndims(dat.x) == 2 % cnt
if ndims(X) == 2 % cnt
for chan=1:sz(2)
[S,F,T] = spectrogram(X(:,chan),opt.Window,opt.NOverlap,freq,dat.fs);
dat.x(:,:,chan)=S;
end
elseif ndims(dat.x)==3 % epoched
elseif ndims(X) == 3 % epoched
for chan=1:sz(3)
for seg=1:sz(2)
[S,F,T] = spectrogram(X(:,seg,chan),opt.Window,opt.NOverlap,freq,dat.fs);
Expand All @@ -122,19 +122,18 @@
dat.zUnit = 'Hz';

switch(opt.Output)
case 'complex'
% do nothing
case 'amplitude'
dat.x = abs(dat.x);
dat.yUnit= 'amplitude';
case 'power'
dat.x = abs(dat.x).^2;
dat.yUnit= 'power';
case 'db'
dat.x = 10* log10( abs(dat.x).^2 );
dat.yUnit= 'log power';
case 'phase'
dat.x = angle(dat.x);
dat.yUnit= 'phase';
end

case 'complex'
% do nothing
case 'amplitude'
dat.x = abs(dat.x);
dat.yUnit= 'amplitude';
case 'power'
dat.x = abs(dat.x).^2;
dat.yUnit= 'power';
case 'db'
dat.x = 10* log10( abs(dat.x).^2 );
dat.yUnit= 'log power';
case 'phase'
dat.x = angle(dat.x);
dat.yUnit= 'phase';
end
4 changes: 2 additions & 2 deletions startup_bbci_toolbox.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function startup_bbci_toolbox(varargin)

% import dependencies if not done yet
if ~exist(fullfile(BTB.Dir, 'external', 'auto_import_performed'), 'file')
inp = input(sprintf(['\nThis is the first startup of the BBCI toolbox - Wellcome!\n\n', ...
inp = input(sprintf(['\nThis is the first startup of the BBCI toolbox - Welcome!\n\n', ...
'You can now automatically download and import code from other sources.\n', ...
'This adds additional functionalities such as ICA and SSD to the BBCI toolbox.\n',...
'Following startups won''t ask for automatic imports. Manual imports are\n',...
Expand Down Expand Up @@ -140,4 +140,4 @@ function startup_bbci_toolbox(varargin)



evalin('base', 'global BTB');
evalin('base', 'global BTB');

0 comments on commit ae77edf

Please sign in to comment.