bandpass_and_hilbert.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. %%% copyright 2012, Emilio Kropff
  2. %%% output: first dim = number of frequencies
  3. %%% second dim = number of trials
  4. %%% third dim = number of timestamps
  5. function h=bandpass_and_hilbert(x,flow,fhigh,sampling)
  6. l=size(x,1);
  7. NFFT= 2^nextpow2(l); % power of 2 is a faster fft
  8. x_fft=fft(x,NFFT);
  9. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. % Hilbert Transform, taken from the MATLAB function hilbert(). h is
  11. % the 'filter' applied on the fft of the signal. The filter
  12. % consists in multiplying the fft by i*sign(freq). But the output
  13. % is hilbert(x)=x-i*transform(x), which results in a total filter that multyplies
  14. % by 2 positive frequencies and by 0 negative frequencies (and by 1 the
  15. % two central frequencies).
  16. % http://en.wikipedia.org/wiki/Hilbert_transform#Relationship_with_
  17. % the_Fourier_transform
  18. hilbert_filter = zeros(NFFT,1);
  19. % NFFT is even (it is a power of 2) so there are two central frequencies
  20. hilbert_filter([1 NFFT/2+1]) = 1;
  21. hilbert_filter(2:NFFT/2) = 2; %positive freqs (upper part of the circle)
  22. fft_freqs=sampling/2*(linspace(0,1,NFFT/2+1))'; %This includes only the upper part of the circle
  23. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  24. % Bandpass Filter
  25. bandpass_filter=bsxfun(@gt,fft_freqs,flow) & bsxfun(@lt,fft_freqs,fhigh);
  26. bandpass_filter=[bandpass_filter; bandpass_filter(end-1:-1:2,:)];
  27. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  28. % Finally, perform the bandpass and hilbert transform in one step
  29. % (blocked)
  30. % nfreqmax=floor(.5*10^8/numel(x_fft));
  31. nfreqmax=floor(.1*10^8/numel(x_fft));
  32. fstart=1; fend=min(numel(flow),fstart-1+nfreqmax);
  33. h=NaN(l,numel(flow));
  34. while fstart<=fend
  35. haux=ifft(bsxfun(@times,x_fft,bsxfun(@times,bandpass_filter(:,fstart:fend),hilbert_filter)));%combined_filter(:,ones(1,car.nlaps)));
  36. h(:,fstart:fend)=haux(1:l,:);
  37. fstart=fend+1;
  38. fend=min(numel(flow),fstart-1+nfreqmax);
  39. end
  40. % h=squeeze(h);
  41. % if nargout>1
  42. % bpx=zeros(nf,nx,l);
  43. % for f=1:nf
  44. % bandpass_filter=(fft_freqs>flow).*(fft_freqs<fhigh);
  45. % bandpass_filter=[bandpass_filter; bandpass_filter(end-1:-1:2)];
  46. % hilbert_transf=ifft(bsxfun(@times,x_fft,bandpass_filter));
  47. % bpx(f,:,:)=hilbert_transf(1:l,:);
  48. % end
  49. % bpx=squeeze(bpx);
  50. % end