y=nt_dsample(x,factor) - downsample by averaging neighboring samples y: downsampled data x: data to downsample (2 or 3D) factor: downsampling factor Downsampling is performed along columns. If size(x,1) is not multiple of factor, it is truncated. The data are lowpass filtered by convolution with a square window, which ensures minimal temporal distortion of the waveform. However it does not strongly attenuate frequency components beyond the Nyquist frequency, so it is not optimal from a frequency-domain point of view. If this is a concern, uses nt_resample() instead. NoiseTools
0001 function x=nt_dsample(x,factor) 0002 %y=nt_dsample(x,factor) - downsample by averaging neighboring samples 0003 % 0004 % y: downsampled data 0005 % 0006 % x: data to downsample (2 or 3D) 0007 % factor: downsampling factor 0008 % 0009 % Downsampling is performed along columns. If size(x,1) is not multiple of 0010 % factor, it is truncated. 0011 % 0012 % The data are lowpass filtered by convolution with a square window, which 0013 % ensures minimal temporal distortion of the waveform. However it does not 0014 % strongly attenuate frequency components beyond the Nyquist frequency, so 0015 % it is not optimal from a frequency-domain point of view. If this is a 0016 % concern, uses nt_resample() instead. 0017 % 0018 % NoiseTools 0019 0020 if nargin<2; error('!'); end 0021 if factor==1; return; end 0022 if factor ~= round(factor); error('factor must be integer'); end 0023 0024 if ndims(x)>2; 0025 d=size(x); 0026 x=reshape(x,[d(1),prod(d(2:end))]); 0027 x=nt_dsample(x,factor); 0028 x=reshape(x,[size(x,1),d(2:end)]); 0029 return 0030 end 0031 0032 [m,n]=size(x); 0033 a=floor(m/factor); 0034 b=rem(m,factor); 0035 0036 if b>0; 0037 xx=x((a*factor+1):end,:); % extra bit 0038 x=x(1:a*factor,:); 0039 end 0040 0041 x=reshape(x,[factor,a,n]); 0042 x=mean(x,1); 0043 x=shiftdim(x,1); 0044 0045 % buggy, dunno why, simpler to remove 0046 % if b>0 0047 % x=[x;mean(xx,1)]; 0048 % end 0049