Bandpass Filters and Hilbert Transform
Total Page:16
File Type:pdf, Size:1020Kb
Bandpass filters and Hilbert Transform Summary of Chapter 14 In Analyzing Neural Time Series Data: Theory and Practice Lauritz W. Dieckman A quick message from Mike X I've created a googlegroups for the book. If you have other questions about the book, analyses, or code, feel free to post them there. https://groups.google.com/forum/#!forum/a nalyzingneuraltimeseriesdata Filters ‐Everywhere • Music ‐analog recordings ~44,100 Hz sampling ‐typically bandpass filtered to range of human hearing ‐Or, purposefully Used in artistic creation ‐One persons distortion is another persons art Why? Filter‐Hilbert Method vs Complex Wavelet Convolution Similarities • Used to create a complex (real and imaginary) time series (analytic signal) from real signal data • Analytic signal used to determine phase and power ‐Methods described in Chapter 13 • The signal must be bandpass filtered before Why? Filter‐Hilbert Method vs Complex Wavelet Convolution Differences • Signal must first be bandpass filtered • Analytic signal acquired by creating the “phase quadrature component” (one quarter‐cycle) by rotating sections of a Fourier spectrum. • PRO: Filter‐Hilbert provides superior control over frequency filtering ‐not limited in shape whereas bandpass filters can take many shapes • CON: However, Filter‐Hilbert requires signal processing toolbox for kernel creation (unlike Morlet wavelets). • CON: Bandpass filtering is slower than wavelets Bandpass Filtering • First step is Bandpass filtering ‐Don’t worry Hilbert fans, we’ll get back to Hilbert transform soon • Highly recommended to separate frequencies before the transform ‐e.g. lower frequencies may dominate combined signal Filters: To Infinity and… much shorter • Finite Impulse Response (FIR) SOON I will reign –The responsesupreme to an and impulse ends at some point.infinite over your ‐Stability analytic signal ‐Computation time ‐ Risk of phase distortions You will never triumph Infinite ImpulseButterworth. Response (IIR) –The responseIt may to takean impulse more than just my does not end.analytic skill to protect the ‐Butterworthsignal. IIR filter Matlab help me! Filters: Types • High‐pass filter – Allows frequencies higher than cutoff to remain in the signal (lower frequency signals are attenuated) ‐Simple illustration –Cutoff 5 12345678 FL studio example Filters: Types • Low‐pass filter – Allows frequencies lower than cutoff to remain in the signal (higher frequency signals are attenuated) ‐Simple illustration –Cutoff 5 12345678 Filters: Types • Band‐Stop filter – Attenuates or removes frequencies within a specific range. ‐e.g. Notch filters at 60 Hz ‐Simple illustration –Low Cutoff 5 & High Cutoff 2 12345678 Filters: Types • Bandpass filter – Allows frequencies lower than lower bound cutoff to remain in the signal and frequencies higher than higher bound cut off to remain in the signal ‐Simple illustration –Low Cutoff 5 & High Cutoff 2 12345678 ‐Basically a high and low‐pass combo LFO Many Music FX use filters Filter • Phaser‐ Separates signal 1. Unfiltered 2. Alters phase ‐When recombined the signals out of phase nullify ‐Frequently combined with low frequency ossiclator How to: Filter Kernel Construction • Like complex wavelet convolution –The analytic signal produced from filtering *weighted combination of kernel and signal How to: Filter Kernel Construction • Note difference shape Gaussian vs Plateau • Plateau shapes allow for greater frequency specificity Filter Functions • Firls – 3 inputs A. Order : Length of filter kernel (+1) *Important for frequency precision Larger Frequency precision + processing time – For a particular frequency of interest: MUST be long enough for one cycle *Recommended 2 –5 time longer than frequency of interest *Use sample points, not time (i.e. must know sample rate) filter_order =round(3*(EEG.srate/lower_filter_bound)); Filter Functions • Firls – 3 inputs B. 6 numbers define shape of response 1. Zero frequency Filter Functions • Firls – 3 inputs to define shape of response B. 6 numbers define shape of response 1. Zero frequency • ffrequencies = [ 0 (1‐transition_width) *lower_filter_bound lower_filter_bound upper_filter_bound (1+transition_width) *upper_filter_bound nyquist ]/nyquist; Filter Functions • Firls – 3 inputs B. 6 numbers define shape of response 2. Frequency of start –lower transition zone Filter Functions • Firls – 3 inputs B. 6 numbers define shape of response 2. Frequency of start –lower transition zone transition_width = 0.2; lower_filter_bound = 4; % Hz ffrequencies = [ 0 (1‐transition_width) *lower_filter_bound lower_filter_bound upper_filter_bound (1+transition_width) *upper_filter_bound nyquist ]/nyquist; Filter Functions • Firls – 3 inputs B. 6 numbers define shape of response 3. Lower bound of bandpass Filter Functions • Firls – 3 inputs B. 6 numbers define shape of response 3. Lower bound of bandpass ffrequencies = [ 0(1‐transition_width) *lower_filter_bound lower_filter_bound upper_filter_bound (1+transition_width) *upper_filter_bound nyquist ]/nyquist; Filter Functions • Firls – 3 inputs B. 6 numbers define shape of response Side note: Transition zones Transition zones Filter Functions • Firls – 3 inputs B. 6 numbers define shape of response 4. Upper bound of bandpass Filter Functions • Firls – 3 inputs B. 6 numbers define shape of response 4. Upper bound of bandpass upper_filter_bound = 10; % Hz ffrequencies = [ 0(1‐transition_width) *lower_filter_bound lower_filter_bound upper_filter_bound (1+transition_width) *upper_filter_bound nyquist ]/nyquist; Filter Functions • Firls – 3 inputs B. 6 numbers define shape of response 5. Frequency of the end of the upper transition zone Filter Functions • Firls – 3 inputs B. 6 numbers define shape of response 5. Frequency of the end of the upper transition zone ffrequencies = [ 0(1‐transition_width) *lower_filter_bound lower_filter_bound upper_filter_bound (1+transition_width) *upper_filter_bound nyquist ]/nyquist; Filter Functions • Firls – 3 inputs B. 6 numbers define shape of response 6. Nyquist frequency Filter Functions • Firls – 3 inputs B. 6 numbers define shape of response 6. Nyquist frequency nyquist = EEG.srate/2; ffrequencies = [ 0(1‐transition_width) *lower_filter_bound lower_filter_bound upper_filter_bound (1+transition_width) *upper_filter_bound nyquist ]/nyquist; Filter Functions • Firls – 3 inputs B. 6 numbers define shape of response ‐ These numbers are scaled so that Nyquist frequency is 1. ‐Once frequencies are set in Hz we divide by the nyquist frequency ffrequencies = [ 0(1‐transition_width) *lower_filter_bound lower_filter_bound upper_filter_bound (1+transition_width) *upper_filter_bound nyquist ]/nyquist; Filter Functions • Firls – 3 inputs C. Ideal response amplitude Length of ideal response should = 6 numbers define shape of response 0s = attenuate . 1s = keep. Can use range 0‐1 [0 0 1 1 0 0] Lower Upper Frequency bounds Filter Functions • Firls – 3 inputs C. Ideal response amplitude Length of ideal response should = 6 numbers define shape of response [0 0 1 1 0 0] DC Nyquist Lower Upper Frequency bounds of transition zones Frequency Width • Controls Time Frequency trade off Narrow Width Frequency Precision Temporal Precision Wide Width Temporal Precision Frequency Precision Time Freq Precision in Width– Time Frequency Wrestling: Fun with mixed metaphors • Unlike combat axiom‐ smaller does not mean quicker • Narrow filter kernels are long and thus influenced by more numbers. Like holding up sumo meat? • Wider width filters have smaller kernels and are thus created more quickly. Like sumo quickly crushing a kid? Precision in Width– Time Frequency Sharp Edges make Ripples in Time <‐ Artifact with a sharp edge Sharp filter edges in frequency increase response, but also increase risk of artifacts in time Shoot for 10‐25% of upper and lower bond firls vs fir1 • Fir 1 has automatic transition zones then smooths • Firls can produce a similar effect if kernel is smoothed with Hamming (or Hann) window See the same… minus the ham Prof. X’s recommendations To make a narrow band filter, use fir1 If you are worried about ripple artifacts Patrick_Stewart = firls can allow for disp(‘To make a narrow band filter, gentle transition zones use fir1. If you are worried about ripple Gentle sloping artifacts, firls can plateaus… allow for gentle gentle filters… transition zones… gentle gentle.’) Check.Your.Filters: So Whatcha Want? Goodness quantified! Ideal forms: Similarity between actual filter and the ideal filter Sum of squared errors Sum of squared errors (SSE) center_freq = 60; % in Hz filter_frequency_spread_wide = 10; % Hz +/‐ the center frequency ffrequencies = [ 0 (1‐transition_width)*(center_freq‐ filter_frequency_spread_wide) (center_freq‐filter_frequency_spread_wide) (center_freq+filter_frequency_spread_wide) (1+transition_width)*(center_freq+filter_frequency_spread_wide) nyquist ]/nyquist; idealresponse = [ 0 0 1 1 0 0 ]; filterweightsW = zscore(firls(200,ffrequencies,idealresponse)); num2str(sum( (idealresponse‐fft_filtkern(freqsidx)).^2 )) SSE = num2str(sum( (idealresponse‐fft_filtkern(freqsidx)).^2 )) Good Filter/Bad Filter: Filter Width SSE should be close to 0 Do not use a filter with SSE > 1 ‐Good at 10hz ‐Bad at 15hz Application: filtfilt • After creating the perfect kernel and testing it • Filtfilt used to apply data2filter = squeeze(double(EEG.data(47,:,1))); filterweights = firls(200,ffrequencies,idealresponse); % recompute without z‐scoring filter_result = filtfilt(filterweights,1,data2filter); Filter Kernel ‐‐ Weight coefficient ‐‐ Data Filtfilt compared to