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