Home > NoiseTools > nt_mean_over_trials.m

# nt_mean_over_trials

## PURPOSE

[y,tw]=nt_mean_over_trials(x,w) - weighted average over trials

## SYNOPSIS

function [y,tw]=nt_mean_over_trials(x,w)

## DESCRIPTION

    [y,tw]=nt_mean_over_trials(x,w) - weighted average over trials

      y: weighted average over trials (time*trials)
      tw: total weight (time*1)

      x: data to average (time*channels*trials)
      w: weight to apply (time*channels*trials or time*1*trials);

## CROSS-REFERENCE INFORMATION

This function calls:

- nt_fold y=fold(x,epochsize) - fold 2D to 3D
- nt_unfold y=nt_fold(x) - unfold 3D to 2D
- nt_vecmult y=nt_vecmult(x,v) - multiply all rows or columns of matrix by vector

This function is called by:

- nt_dss1 [todss,pwr0,pwr1]=nt_dss1(x,w,keep1,keep2,sns_flag) - evoked-biased DSS denoising

## SOURCE CODE

    0001 function [y,tw]=nt_mean_over_trials(x,w)
    0002 %[y,tw]=nt_mean_over_trials(x,w) - weighted average over trials
    0003 %
    0004 %  y: weighted average over trials (time*trials)
    0005 %  tw: total weight (time*1)
    0006 %
    0007 %  x: data to average (time*channels*trials)
    0008 %  w: weight to apply (time*channels*trials or time*1*trials);
    0009
    0010 if nargin<2; w=[]; end
    0011 if nargin<1; error('!'); end
    0012
    0013 [m,n,o]=size(x);
    0014
    0015 if isempty(w);
    0016     y=mean(x,3);
    0017     tw=ones(m,n,1)*o;
    0018 else
    0019     [mw,nw,ow]=size(w);
    0020     if mw~=m; error('!'); end
    0021     if ow~=o; error('!'); end
    0022     x=nt_unfold(x);
    0023     w=nt_unfold(w);
    0024     if nw==n;
    0025         x=x.*w;
    0026         x=nt_fold(x,m);
    0027         w=nt_fold(w,m);
    0028         y=sum(x,3)./sum(w,3);
    0029     elseif nw==1;
    0030         x=nt_vecmult(x,w);
    0031         x=nt_fold(x,m);
    0032         w=nt_fold(w,m);
    0033         y=nt_vecmult(sum(x,3),1./sum(w,3));
    0034     end
    0035     tw=sum(w,3);
    0036 end
    0037