Biomedical Signals Signals and Images in Medicine Dr Nabeel Anwar
Noise Removal: Time Domain Techniques 1. Synchronized Averaging (covered in lecture 1) 2. Moving Average Filters (today s topic) 3. Derivative based operations to remove low frequency artifacts (today s topic) 4. Introduction to frequency domain
Moving Average Filters Problem: Propose a time-domain technique to remove random noise given only one realization of the signal or event of interest.
Moving Average (MA) Filter Ensemble of several realizations not available synchronized averaging is not possible. Consider temporal averaging for noise removal Temporal window of samples moved to obtain output at various points of time: moving-window averaging or movingaverage (MA) filter. Average weighted combination of samples
Moving Average (MA) Filter Rangaraj s book 3.3.2 x and y: input and output of filter. b k : filter coefficients or tap weights. N: order of filter.
Moving Average (MA) Filter Basic Concept OF Z-Transform Z-Transform is a digital operation When identified with a digital data sequence, such as x(n), z n represents an interval shift of n samples, or an associated time shift of nts seconds. Every data sample in the sequence x(n) is associated with a unique power of z, and this power of z defines a sample s position in the sequence. For example, the time shifting characteristic of the Z-transform can be used to define a unit delay process, z 1. For such a process, the output is the same as the input, but shifted (or delayed) by one data sample
Delay of one Sample Consider the system y[n] = x[n-1], i.e., the onesample-delay system. The z-transform system function is H 1 z z Z -1
Delay of k Samples Similarly, the system y[n] = x[n-k], i.e., the k-sampledelay system, is the z-transform of the impulse response x[n - k]. H z z k Z -k
Moving Average (MA) Filter Signal-flow diagram of a moving-average filter of order N. Each block with the symbol z 1 represents a delay of one sample, and serves as a memory unit for the corresponding signal sample value.
Example: Running Average Algorithm Block Diagram Transfer Function 4 2 3 3 2 1 4 1 4 1 z z z z z X z z z z X z Y 4 2 3 4 1 z z z z z X Y 4 3 2 1 k k k k k x x x x y Note: Each [Z -1 ] block can be thought of as a memory cell, storing the previously applied value. (Non-Recursive) Z Transform
Exercise
Moving Average (MA) Filter Moving Average can be symmetrical or non-symmterical. e.g. Y[80] = ( x[80] + x[79]+x[78] +x[77]) / 4 : nonsymmterical. Y[80] = ( x[79] + x[80]+x[81] ) / 3 : symmterical A potential problem is look-ahead
Moving Average (MA) Filter See Fig 15-1 in DSP Guide
Moving Average (MA) Filter Examples of moving average Moving average term is used for any process that uses a moving set of multiplier weights. That means we have finite number of weights. It gives the concept that An MA filter is a finite impulse response (FIR) filter: Output 'may depends only on the present input sample and a few past input samples.
MA: Application Example ECG signal with 1000 Hz noise 8 points moving average filter
Moving average is Recursive A tremendous advantage of the moving average filter is that it can be implemented with an algorithm that is very fast.
Assignment 2 Apply an 8 point moving average filter on the raw ECG data already provided. Apply a 32 point filter on the raw ECG data. What is the difference? Does 32 point filter preserved information?
Frequency Analysis
Next few slides are taken from book Advanced Engineering Mathematics by Erwin Kreyszig John Wiley & Sons, Inc. Consult the book for further info. Pages 478-479b
Advanced Engineering Mathematics by Erwin Kreyszig Copyright 2007 John Wiley & Sons, Inc. All rights reserved.
Pages 480-482c Advanced Engineering Mathematics by Erwin Kreyszig Copyright 2007 John Wiley & Sons, Inc. All rights reserved.
Fourier Transform Before we start, lets discuss a little about Fourier Transform. The basic idea is: A signal can be decomposed into periodic cosine and sine waves with different frequencies. Fourier Analysis has four categories (Slide 33-34: taken from Smith s book)
Frequency Spectra example : g(t) = sin(2πf t) + (1/3)sin(2π(3f) t) = +
Frequency Spectra =
Example: Music We think of music in terms of frequencies at different magnitudes Time information is LOST
Fourier Transforms All of us has basic concept of functions. In signal processing, most functions fall into two categories: 1. waveforms, images, or other data; 2. and entities that operate on waveforms, images, or other data The later group can be further divided into functions i. that modify the data, ii. and functions used to analyze or probe the data. For example, the basic filters use functions (the filter coefficients) that modify the spectral content of a waveform. The Fourier Transform detailed uses functions (harmonically related sinusoids) to analyze the spectral content of a waveform. Functions that modify data are also termed operations or transformations.
Fourier Transform A transform can be thought of as a re-mapping of the original data into a function that provides more information than the original. The Fourier Transform is classic example as it converts the original time data into frequency information which often provides greater insight into the nature and/or origin of the signal.
Fourier Transform How we do that?... Many of the transforms are achieved by comparing the signal of interest with some sort of probing function. This comparison takes the form of a correlation (produced by multiplication) that is averaged (or integrated) over the duration of the waveform, or some portion of the waveform:
Fourier Transform It is a technique for examining signals in the frequency domain. Our immediate goal is to represent a given function as a convergent series in the elementary trigonometric functions (already studied them few slides before) f(x) Fourier Transform F(w)
Fourier Analysis f ( t) a 2 0 n 1 a n cos 2 nt T n 1 b n sin 2 nt T DC Part Even Part Odd Part T is a period of all the above signals with the first term ½ a 0 representing the direct current (DC) component; the remaining sine and cosine waves weighted by the a n and b n coefficients represent the alternating current (AC) components of the signal. It is very important to find correct amplitude of a o, a n and b n
a o can be calculated by b n all coefficients are zero except b n General formula for a n
Fourier Analysis (in words) The above expression has a real part of cosine of frequency f, and an imaginary part of sine of frequency f. So what we are actually doing is, multiplying the original signal with a complex expression which has sines and cosines of frequency f. Then we integrate this product (In other words, we add all the points in this product). If the result of this integration (which is nothing but some sort of infinite summation) is a large value, then we say that : the signal x(t), has a dominant spectral component at frequency "f". This means that, a major portion of this signal is composed of frequency f. If the integration result is a small value, than this means that the signal does not have a major frequency component of f in it. If this integration result is zero, then the signal does not contain the frequency "f" at all. How this integration works: The signal is multiplied with the sinusoidal term of frequency "f". If the signal has a high amplitude component of frequency "f", then that component and the sinusoidal term will coincide, and the product of them will give a (relatively) large value. This shows that, the signal "x", has a major frequency component of "f". However, if the signal does not have a frequency component of "f", the product will yield zero, which shows that, the signal does not have a frequency component of "f". If the frequency "f", is not a major component of the signal "x(t)", then the product will give a (relatively) small value. This shows that, the frequency component "f" in the signal "x", has a small amplitude, in other words, it is not a major component of "x.
Fourier: Examples The typical syntax for computing the FFT of a signal is FFT(x,N) where x is the signal, x[n], you wish to transform, and N is the number of points in the FFT. N must be at least as large as the number of samples in x[n]. Matlab Example: To demonstrate the effect of changing the value of N, i.e 64, 128,256, 512 Matlab example
Fourier: Examples 20 N = 16 10 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 N = 32 20 10 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 N = 64 20 10 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 N = 128 20 10 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 N = 256 20 10 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Fourier: Examples In the previous slide Upon examining the plot one can see that each of the transforms adheres to the same shape, differing only in the number of FFT samples used to approximate that shape. But once the number of FFT points are very small the shape is different (N=16 and N=32). When the FFT is computed with an N larger than the number of samples in x[n], it fills in the samples after x[n] with zeros. For example if x[n] is 30 samples long, and the length FFT is 256. When Matlab computes the FFT, it automatically fills the spaces from n= 30 to n = 255 with zeros. This is called zero padding.
Fourier: Examples 1. The FFT does not directly give you the spectrum of a signal. As we have seen with the last two examples, the FFT can vary dramatically depending on the number of points (N) of the FFT, and the number of periods of the signal that are represented. 2. The FFT contains information between 0 and fs, however, we know that the sampling frequency must be at least twice the highest frequency component. Therefore, the signal's spectrum should be entirly below fs/2, the Nyquist frequency. 3. Also real signal should have a transform magnitude that is symmetrical for positive and negative frequencies. So instead of having a spectrum that goes from 0 to fs, it would be more appropriate to show the spectrum from fs/2 to fs/2.
Fourier: Examples 16 14 12 10 8 6 4 2 0-0.5-0.4-0.3-0.2-0.1 0 0.1 0.2 0.3 0.4 0.5 frequency / f s
A bit More on FFT It is clear that fft gives you a two sided spectrum. The basic computations for analyzing signals include converting from a two-sided power spectrum to a single-sided power spectrum. Converting from a Two-Sided Power Spectrum to a Single- Sided Power Spectrum In a two-sided spectrum, half the energy is displayed at the positive frequency, and half the energy is displayed at the negative frequency. Therefore, to convert from a two-sided spectrum to a single-sided spectrum, discard the second half of the array and multiply every point except for DC by two.
DC component remains same Others are multiplied by 2.
FFT: Spectrum Type and Scaling FFT returns complex-valued amplitudes Real part represents cosine components, imaginary part represents sine components (90 phase difference) Can be converted to magnitude and phase Squared magnitude represents signal power
Limitations of Fourier Analysis 1. Cannot not provide simultaneous time and frequency localization. 2. Not useful for analyzing time-variant, non-stationary signals. 3. Not efficient for representing discontinuities or sharp corners (i.e., requires a large number of Fourier components to represent discontinuities).
Limitations of Fourier Analysis f 4 ( t) cos(2 5 t) cos(2 25 t) cos(2 50 t) Provides excellent localization in the frequency domain but poor localization in the time domain.
Limitations of Fourier Analysis 1. Cannot not provide simultaneous time and frequency localization. 2. In its current form Not useful for analyzing time-variant, nonstationary signals. 3. Not appropriate for representing discontinuities or sharp corners (i.e., requires a large number of Fourier components to represent discontinuities).
Stationary vs non-stationary signals Stationary signals: timeinvariant spectra Non-stationary signals: time-varying spectra
Stationary vs non-stationary signals Stationary signal: Three frequency components, present at all times!
Stationary vs non-stationary signals Non-stationary signal: The reason of the noise like thing in between peaks show that, those frequencies also exist in the signal. But the reason they have a small amplitude, is because, they are not major spectral components of the given signal, and the reason we see those, is because of the sudden change between the frequencies.
Short Time Fourier Transform What's the solution to tackle the problems of non-stationary signal? We Need a local analysis scheme for a time-frequency representation. Windowed F.T. or Short Time F.T. (STFT) Segmenting the signal into narrow time intervals (i.e., narrow enough to be considered stationary). Take the Fourier transform of each segment.
Short Time Fourier Transform Output = fft(signal,nfft_length); Above statement is from MATLAB, it has two variables namely Signal: is a signal (ECG/EMG ) which has some finite data points. Nfft_length: is length of fft you want to implement. So your output is dependent upon both signal length and nfft_length. Basically, the FFT size can be defined independently from the window size. The FFT size defines the number of bins used for dividing the window into equal strips, or bins. Hence, a bin is a spectrum sample, and defines the frequency resolution of the window. A window size is expressed in samples. This parameter is a variable. Practically, there is a relationship between the number of samples of the window and duration of the window.
Short Time Fourier Transform Suppose I want to analyze a frequency component of 10 Hz. That gives me time period of 100ms. In general we need 4~5 cycles to estimate the frequency component reasonably. That gives you 500 ms of time window of signal. Similarly for 250 Hz frequency you need 20 ms. Lower the frequency the bigger the window size should be.
Short Time Fourier Transform Let's take a 44100 sampling rate (SR). SR=44100 Hz, F(max) = 22050 Hz. (according to Nyquist theorm) Case1: With a 1024 window size, we get. FR = 44100/1024 = 43.066 Hz The spectrum is equally split into bins of 43.066 Hz width. Case2: If we choose a 4096 window size we get FR = 44100/4096 = 10.76 Hz The spectrum is equally split into bins of 10.76 Hz width. The frequency resolution is more precise. The more bins, the more slices of frequency range we get, and the more precise these slices are.
Short Time Fourier Transform Steps : 1. Choose a window function of finite length 2. Place the window on top of the signal at t=0 3. Truncate the signal using this window 4. Compute the FT of the truncated signal, save results. 5. Incrementally slide the window to the right 6. Go to step 3, until window reaches the end of the signal
Short Time Fourier Transform Each FT provides the spectral information of a separate timeslice of the signal, providing simultaneous time and frequency information
Short Time Fourier Transform Time parameter Frequency parameter Signal to be analyzed 1D 2D u 2 (, ) ( ) ( ) j STFT ut f t u f t W t t e dt t STFT of f(t): computed for each window centered at t=t Windowing function centered at t=t
Choosing Window W(t) What shape should it have? Rectangular, Gaussian, Elliptic? How wide should it be? Window should be narrow enough to make sure that the portion of the signal falling within the window is stationary. Very narrow windows do not offer good localization in the frequency domain.
STFT Window Size u 2 (, ) ( ) ( ) j STFT ut f t u f t W t t e dt W(t) infinitely long: W ( t) 1 STFT turns into FT, providing excellent frequency localization, but no time information. W(t) infinitely short: W ( t) ( t) gives the time signal back, with a phase factor, providing excellent time localization but no frequency information. t 2 STFT ( t, u) f ( t) ( t t ) e dt f ( t ) e u j ut jut f t
STFT Window Size (cont d) Wide window good frequency resolution, poor time resolution. Narrow window good time resolution, poor frequency resolution. In next lectures: We will study different windowing types/shapes.
Example Use this link for an Excellent work of Polikar on STFT and wavelets http://users.rowan.edu/~polikar/wavelets/wtpart2.html different size windows (four frequencies, non-stationary)
Example
Example
Example: Effect of FFT window Consider following scenario Two sin waves have frequencies of 100 Hz and 110 Hz, and the sampling rate is 1 khz. Apply a FFT with window length 64 samples and 1024 samples. Solution: We experiment with two time windows of length N1=1024 with a theoretical frequency resolution of f=1000/1024=0.98 Hz, and N2=64 with a theoretical frequency resolution f=1000/64=15.7 Hz.
Heisenberg (or Uncertainty) Principle t f 1 4 Time resolution: How well two spikes in time can be separated from each other in the transform domain. Frequency resolution: How well two spectral components can be separated from each other in the transform domain t and f cannot be made arbitrarily small!
Heisenberg (or Uncertainty) Principle One cannot know the exact time-frequency representation of a signal. We cannot precisely know at what time instance a frequency component is located. We can only know what interval of frequencies are present in which time intervals.
The Effect of Finite Length Data (Windowing) We already know that In practical situation we have either with a short length signal, or with a long signal. Having a short segment of N samples of a signal or taking a slice of N samples from a signal is equivalent to multiplying the signal by a window (We have considered Gaussian shape in example) of N samples. Multiplying two signals in time is equivalent to the convolution of their frequency spectra. Thus the spectrum of a short segment of a signal is convolved with the spectrum of a rectangular pulse, the result of this convolution is some spreading of the signal energy.
Assignment Take the FFT of complete single trial signal. Already provided to you. Remember that FFT has both Real and Imagery Parts. So take real part only, you can also use abs function. Length of FFT window should also be the part of your concept. Vary the fft length and look into its influence on the signal.
Fs = 150; % Sampling frequency t = 0:1/Fs:1; % Time vector of 1 second f = 5; % Create a sine wave of f Hz. x = sin(2* pi*t*f); % x = cos(2* pi*t*f); %x = square(2* pi*t*f); nfft = 1024; % Length of FFT % Take fft, padding with zeros so that length(x) X = fft(x,nfft); % second 0 0.2 0.4 0.6 0.8 1 %Power Spectrum of a Sine Wave FFT is symmetric, throw away half X = X(1:nfft/2); % Take the magnitude of fft of x mx = abs(x); % Frequency vector f = (0:nfft/2-1)*Fs/nfft; % Generate the plot, title and labels. subplot (2,1,1) plot(t,x); title('sine Wave Signal'); xlabel('time (s)'); ylabel('amplitude'); subplot(2,1,2) plot(f,mx); title('power Spectrum of a Sine Wave'); xlabel('frequency (Hz)'); ylabel('power');
Spectrum Types Magnitude: Amplitude Spectrum Squared magnitude: Power Spectrum Squared magnitude per unit bandwidth: Power Spectral Density Squared magnitude block time length: Energy Spectrum Squared magnitude block length per unit bandwidth: Energy Spectral Density
Spectrum Types: When to use Periodic signals (discrete frequencies): Amplitude or Power Spectrum Broadband random signals: Power Spectral Density Transient Signals: Energy Spectral Density
Averaging in Spectrum Combine multiple time blocks together to form one spectral estimate Random data: higher average number, better estimate of random characteristics
Flow diagram