Bandpass filters and

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 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 convolution Phase Delay and Time Travel

• Causal filter : Filter is based on information from the past (back in time) • Filter function: Causes a phase delay Phase Delay and Time Travel

• Luckily we can filter back in time to correct the errors of the past! • Forward_to_the‐past = 1‐(Back_to_the_Future) • Refiltering corrects the phase delay Damn it DOC, my mat‐ I need a nuclear telepathy demands reaction to GIGA! GIGAWATTS generate 1.21 JIGAwatts of electricity. You thought your puny mat‐telepathy powers could defeat me? With my Infinity Impulse Response Filtering death ray I will sow (relative) chaos, instability and non linear phase distortions across your signal! Bwahahaha

NOOOOO! 5th order Butterworth

Hope for analytic signal… fading…must use Hilbert powers to… transform Revenge of the Hilbert Transform

• Remember way back‐ before time traveling filters, sumo quick narrow filters, and (poor) comic book‐ish fight scenes? Concatenation • Analytic signal produced by powers activate! adding phase quadrature (1/4) Hilbert transform!

• Created by rotating aspects of complex frequency spectrum (from Fourier) How to filter‐Hilbert

Hilbert Transformer: With Autobot Matrix 1. from bandpass filtered signal and create an imaginary copy (i.e. Fourier * i) 2. Find the positive (> 0 to Nyquist) and negative (> Nyquist frequencies). *Note: 0 & Nyquist are not used 3. Convert from complex cosine to complex sine by rotating positive 90 degree (‐pi/2) counter clockwise & negative frequencies 90 degrees clockwise Positive Negative 2 x pos Subtract freq coeff from self Original = 0 How to Hilbert

• Final: Inverse Fourier • These ingredients produce the analytic signal • Does not influence real signal Warning: Hilbert command will run FFT on the first dimension in a data matrix. Must be time in the first dimension Double check your signal

• Plot phase angles! Come my allies, we have earned good night’s sleep