Lecture2 Noise Freq (1)

download Lecture2 Noise Freq (1)

of 69

Transcript of Lecture2 Noise Freq (1)

  • 8/18/2019 Lecture2 Noise Freq (1)

    1/69

    Biomedical SignalsSignals and Images in Medicine

    Lecture 2

    Dr Nabeel Anwar

  • 8/18/2019 Lecture2 Noise Freq (1)

    2/69

    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

  • 8/18/2019 Lecture2 Noise Freq (1)

    3/69

    Moving Average Filters

    Problem:

    Propose a time-domain technique to remove random noise

    given only one realization of the signal or event of interest.

  • 8/18/2019 Lecture2 Noise Freq (1)

    4/69

    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 moving-

    average (MA) filter.

    • Average∼weighted combination of samples

  • 8/18/2019 Lecture2 Noise Freq (1)

    5/69

    Rangaraj’s book 3.3.2

    x and y: input and output of filter.

    bk

    : filter coefficients or tap weights.

    N: order of filter.

    Moving Average (MA) Filter

  • 8/18/2019 Lecture2 Noise Freq (1)

    6/69

    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

    Moving Average (MA) Filter

  • 8/18/2019 Lecture2 Noise Freq (1)

    7/69

    Delay of one Sample

    Consider the system y[n] = x[n-1], i.e., the one-sample-delay system.

    The z-transform system function is

      1  z  z  H 

     Z -1

  • 8/18/2019 Lecture2 Noise Freq (1)

    8/69

    Delay of k Samples

      k  z  z  H   

    Similarly, the system y[n] = x[n-k ], i.e., the k-sample-

    delay system, is the z-transform of the impulse

    response x[n - k ].

     Z -k 

  • 8/18/2019 Lecture2 Noise Freq (1)

    9/69

    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.

  • 8/18/2019 Lecture2 Noise Freq (1)

    10/69

    Example: Running Average Algorithm

    Block Diagram Transfer Function

    4

    23321

    4

    1

    4

    1

     z 

     z  z  z  z  X 

     z  z  z  z  X  z Y 

     

    4

    23

    4

    1

     z 

     z  z  z  z 

     X 

    Y   

    4

    321    

      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

  • 8/18/2019 Lecture2 Noise Freq (1)

    11/69

    • Moving Average can be symmetrical or non-symmterical.

    • e.g. Y[80] = ( x[80] + x[79]+x[78] +x[77]) / 4 : non-

    symmterical.

    Y[80] = ( x[79] + x[80]+x[81] ) / 3 : symmterical

    A potential problem is look-ahead

    Moving Average (MA) Filter

  • 8/18/2019 Lecture2 Noise Freq (1)

    12/69

    Moving Average (MA) Filter

    See Fig 15-1 in DSP Guide

  • 8/18/2019 Lecture2 Noise Freq (1)

    13/69

    • 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 depends only on the present input sample and a few

     past input samples.

    Moving Average (MA) Filter

  • 8/18/2019 Lecture2 Noise Freq (1)

    14/69

    MA: Application Example

    ECG signal with 1000 Hz noise

    8 points moving average filter

  • 8/18/2019 Lecture2 Noise Freq (1)

    15/69

    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.

  • 8/18/2019 Lecture2 Noise Freq (1)

    16/69

    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?

  • 8/18/2019 Lecture2 Noise Freq (1)

    17/69

    Frequency Analysis

  • 8/18/2019 Lecture2 Noise Freq (1)

    18/69

    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

  • 8/18/2019 Lecture2 Noise Freq (1)

    19/69

  • 8/18/2019 Lecture2 Noise Freq (1)

    20/69

       A   d  v  a  n  c  e   d   E  n  g   i  n  e  e  r   i  n  g

       M  a   t   h  e  m  a   t   i  c  s   b  y   E  r  w

       i  n   K  r  e  y  s  z   i  g

       C  o  p  y  r   i  g   h   t     

       2   0   0   7   J  o

       h  n   W   i   l  e  y   &

       S  o  n  s ,

       I  n  c .

       A   l   l  r   i  g   h   t  s

      r  e  s  e  r  v  e   d .

  • 8/18/2019 Lecture2 Noise Freq (1)

    21/69

       A   d  v  a  n  c  e   d   E  n  g   i  n  e  e  r   i  n  g

       M  a   t   h  e  m  a   t   i  c  s   b  y   E  r  w

       i  n   K  r  e  y  s  z   i  g

       C  o  p  y  r   i  g   h   t     

       2   0   0   7   J  o

       h  n   W   i   l  e  y   &

       S  o  n  s ,

       I  n  c .

       A   l   l  r   i  g   h   t  s

      r  e  s  e  r  v  e   d .Pages 480-482c

  • 8/18/2019 Lecture2 Noise Freq (1)

    22/69

    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 30-31: taken from

    Smith’s book)

  • 8/18/2019 Lecture2 Noise Freq (1)

    23/69

    Frequency Spectra

    • example : g (t ) = sin(2π  f t ) + (1/3)sin(2π (3f ) t )

    = +

  • 8/18/2019 Lecture2 Noise Freq (1)

    24/69

    =

    Frequency Spectra

  • 8/18/2019 Lecture2 Noise Freq (1)

    25/69

    Example: Music

    • We think of music in terms of frequencies at different

    magnitudes

    Time information is LOST

  • 8/18/2019 Lecture2 Noise Freq (1)

    26/69

  • 8/18/2019 Lecture2 Noise Freq (1)

    27/69

  • 8/18/2019 Lecture2 Noise Freq (1)

    28/69

    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 functionsi. that modify the data,

    ii. and functions used to analyze or probe the data.

    For example, the basic filters use functions (the filter coefficients) thatmodify the spectral content of a waveform.

    The Fourier Transform detailed uses functions (harmonically relatedsinusoids) to analyze the spectral content of a waveform.

    Functions that modify data are also termed operations or transformations.

  • 8/18/2019 Lecture2 Noise Freq (1)

    29/69

    • 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 theoriginal time data into frequency information which often

    provides greater insight into the nature and/or origin of the

    signal.

    Fourier Transform

  • 8/18/2019 Lecture2 Noise Freq (1)

    30/69

    • 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

  • 8/18/2019 Lecture2 Noise Freq (1)

    31/69

    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)     F(   ) Fourier

    Transform

  • 8/18/2019 Lecture2 Noise Freq (1)

    32/69

    nt b

    nt a

    at  f  

    n

    n

    n

    n

     

    2sin

    2cos

    2

    )(

    11

    0

    DC Part Even Part Odd Part

    T is a period of all the above signals

    Fourier Analysis

    with the first term

    ½ a0 representing the direct current (DC) component;

    the remaining sine and cosine waves weighted by the an and bn coefficients

    represent the alternating current (AC) components of the signal. It is very important to

    find correct amplitude of ao , an and bn

  • 8/18/2019 Lecture2 Noise Freq (1)

    33/69

    ao can be calculated by

    bn all coefficients are zero except bn

    General formula for an

  • 8/18/2019 Lecture2 Noise Freq (1)

    34/69

    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 havea 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, thesignal "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“.

  • 8/18/2019 Lecture2 Noise Freq (1)

    35/69

    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

  • 8/18/2019 Lecture2 Noise Freq (1)

    36/69

    Fourier: Examples

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

    10

    20

    N = 64

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

    10

    20

    N = 128

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

    10

    20

    N = 256

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

    10

    20

    N = 16

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

    10

    20

    N = 32

  • 8/18/2019 Lecture2 Noise Freq (1)

    37/69

    Fourier: Examples

    In the previous slide

    • Upon examining the plot one can see that each of the transformsadheres to the same shape, differing only in the number of FFTsamples used to approximate that shape.

    • But once the number of FFT points are very small the shape isdifferent (N=16 and N=32).

    •When the FFT is computed with an N larger than the number ofsamples in x[n], it fills in the samples after x[n] with zeros. Forexample if x[n] is 30 samples long, and the length FFT is 256. WhenMatlab computes the FFT, it automatically fills the spaces from n=30 to n = 255 with zeros. This is called zero padding.

  • 8/18/2019 Lecture2 Noise Freq (1)

    38/69

    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

  • 8/18/2019 Lecture2 Noise Freq (1)

    39/69

    Fourier: Examples

    -0.5 -0.4 -0.3 -0.2 -0.1 0 0.1 0.2 0.3 0.4 0.50

    2

    4

    6

    8

    10

    12

    14

    16

    frequency / f s

  • 8/18/2019 Lecture2 Noise Freq (1)

    40/69

    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-sidedspectrum to a single-sided spectrum, discard the second

    half of the array and multiply every point except for DC by two.

  • 8/18/2019 Lecture2 Noise Freq (1)

    41/69

    DC component

    remains same

    Others are multiplied

    by 2.

  • 8/18/2019 Lecture2 Noise Freq (1)

    42/69

    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

  • 8/18/2019 Lecture2 Noise Freq (1)

    43/69

    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).

  • 8/18/2019 Lecture2 Noise Freq (1)

    44/69

    )502cos(

    )252cos(

    )52cos()(4

    t t  f  

      

      

      

    Provides excellent

    localization in the

    frequency domain

     but poor localization

    in the time domain.

    Limitations of Fourier Analysis

  • 8/18/2019 Lecture2 Noise Freq (1)

    45/69

    1. Cannot not provide simultaneous time and frequency

    localization.

    2. In its current form Not useful for analyzing time-variant, non-stationary signals.

    3. Not appropriate for representing discontinuities or sharp corners

    (i.e., requires a large number of Fourier components to representdiscontinuities).

    Limitations of Fourier Analysis

    St ti t ti

  • 8/18/2019 Lecture2 Noise Freq (1)

    46/69

    Stationary vs non-stationary

    signals

    • Stationary signals: time-

    invariant spectra

    • Non-stationary signals:

    time-varying spectra

    S i i

  • 8/18/2019 Lecture2 Noise Freq (1)

    47/69

    Stationary signal:

    Three frequency

    components,

     present at all

    times!

    Stationary vs non-stationary

    signals

    St ti t ti

  • 8/18/2019 Lecture2 Noise Freq (1)

    48/69

     Non-stationary signal:

    Stationary vs non-stationary

    signals

    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, theyare not major spectral

    components of the given

    signal, and the reason we see

    those, is because of the

    sudden change between the

    frequencies.

  • 8/18/2019 Lecture2 Noise Freq (1)

    49/69

    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

  • 8/18/2019 Lecture2 Noise Freq (1)

    50/69

    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

  • 8/18/2019 Lecture2 Noise Freq (1)

    51/69

    Short Time Fourier Transform

    Each FT provides the spectral information of a separate time-

    slice of the signal, providing simultaneous time and frequency

    information

  • 8/18/2019 Lecture2 Noise Freq (1)

    52/69

      2( , ) ( ) ( )u j ut   f  t 

    STFT t u f t W t t e dt   

    STFT of f(t):

    computed for each

    window centered at t=t’

    Time

     parameter 

    Frequency

     parameter Signal to

     be analyzed

    Windowing

    function

    centered at t=t’

    1D 2D

    Short Time Fourier Transform

  • 8/18/2019 Lecture2 Noise Freq (1)

    53/69

    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 portionof the signal falling within the window is stationary.

    • Very narrow windows do not offer good localization in the

    frequency domain.

  • 8/18/2019 Lecture2 Noise Freq (1)

    54/69

    STFT Window Size

    W(t)  infinitely long: STFT turns into FT, providing excellent frequency localization, but no time

    information.

    W(t)  infinitely short: gives the time signal

     back, with a phase factor, providing excellent time localization butno frequency information.

    1)(   t W 

    )()(   t t W     

      2( , ) ( ) ( ) ( )u j ut jut   f  t 

    STFT t u f t t t e dt f t e    

      2( , ) ( ) ( )u j ut   f  t 

    STFT t u f t W t t e dt    

  • 8/18/2019 Lecture2 Noise Freq (1)

    55/69

    STFT Window Size (cont’d)

    • Wide window good frequency resolution, poor time

    resolution.

    • Narrow window good time resolution, poor frequencyresolution.

    • In next lectures: We will study different windowing

    types/shapes.

  • 8/18/2019 Lecture2 Noise Freq (1)

    56/69

    Example

    different size windows

    (four frequencies, non-stationary)

    Use this link for an Excellent work of Polikar on STFT and wavelets

    http://users.rowan.edu/~polikar/WAVELETS/WTpart2.html

  • 8/18/2019 Lecture2 Noise Freq (1)

    57/69

    Example

  • 8/18/2019 Lecture2 Noise Freq (1)

    58/69

    Example

  • 8/18/2019 Lecture2 Noise Freq (1)

    59/69

    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 lengthN1=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.

    Example: Effect of FFT window

  • 8/18/2019 Lecture2 Noise Freq (1)

    60/69

    Heisenberg (or Uncertainty)

  • 8/18/2019 Lecture2 Noise Freq (1)

    61/69

    Heisenberg (or Uncertainty)

    Principle

      4

    1   f  t 

    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)

  • 8/18/2019 Lecture2 Noise Freq (1)

    62/69

    Heisenberg (or Uncertainty)

    Principle

    • One cannot know the exact time-frequency

    representation of a signal.

    • We cannot precisely know at what time instance afrequency component is located.

    • We can only know what interval of frequencies are

    present in which time intervals.

    The Effect of Finite Length Data

  • 8/18/2019 Lecture2 Noise Freq (1)

    63/69

    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 somespreading of the signal energy.

  • 8/18/2019 Lecture2 Noise Freq (1)

    64/69

    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

  • 8/18/2019 Lecture2 Noise Freq (1)

    65/69

    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');

  • 8/18/2019 Lecture2 Noise Freq (1)

    66/69

    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

  • 8/18/2019 Lecture2 Noise Freq (1)

    67/69

    Spectrum Types: When to use

    • Periodic signals (discrete frequencies): Amplitude or PowerSpectrum

    • Broadband random signals: Power Spectral Density

    • Transient Signals: Energy Spectral Density

  • 8/18/2019 Lecture2 Noise Freq (1)

    68/69

    Averaging in Spectrum

    • Combine multiple time blocks together to form one spectralestimate

    • Random data: higher average number, better estimate of

    random characteristics

  • 8/18/2019 Lecture2 Noise Freq (1)

    69/69

    Flow diagram