42
Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26 th , 2015

Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

Lab 7Data Acquisition, Aliasing, FFT, and

the Optical Fourier Transform

Wente Yin

February 26th, 2015

Page 2: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• DAQ

• DFTs

• DFTs in MATLAB

• Frequency Shifting

• Gibbs Phenomenon

• Aliasing

• Advice

Agenda

Page 3: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Two Sets of Inputs• Analogue (for…analogue)

• Digital (for data transfer, digital sensors, etc.)

• Requires Proper Drivers for Function

Data Acquisition (DAQ)

Page 4: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

Data Acquisition (DAQ, Cont’d)

Page 5: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Computers are Discrete in Nature• Discrete Not continuous, requires individual input

(samples)

• Continuous functions would require infinite amount of RAM, which cannot be downloaded (non-countable infinity).

Discrete Fourier Transforms

Page 6: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Continuous Fourier Transform

𝐹 ξ =

−∞

𝑓 𝑥 exp −𝑗2𝜋𝑥ξ 𝑑𝑥

𝑓 𝑥 =

−∞

𝐹(ξ) exp 𝑗2𝜋𝑥ξ 𝑑𝑥

DFTs (Cont’d)

Page 7: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

DFTs (Cont’d)

• Discrete Fourier Transform

𝐹𝑘 =

𝑛=0

𝑁−1

𝑓𝑛 exp −2𝜋𝑖𝑛𝑘

𝑁

𝑓𝑛 =1

𝑁

𝑘=0

𝑁−1

𝐹𝑘 exp2𝜋𝑖𝑛𝑘

𝑁

Page 8: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

DFTs (Cont’d)

• Functions Must be Discretely Sampled• Allows a simulated Fourier transform

• DFT is cyclic (periodic), and multiple cycles are required for the function to appear in its appropriate dimensions

• Several issues arise with MATLAB’s implementation of the DFT

Page 9: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

DFTs (Cont’d)

• DFT in MATLAB• fft (Single-Dimension), fftn (N-Dimension)

• The fft function will carry out the DFT in a manner that places zero frequencies to the left (DC Bias, Single Dimension) or corners (Two-Dimension)

• fft also limits DFT to a single cycle (appears more like a continuous FT), which is the source of problems like aliasing

Page 10: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

DFTs (Cont’d)

• fft (Fast Fourier Transform)• Function makes use of symmetry of input to reduce

computational complexity from N2 to Nlog2(N)

• As such, it is best to have inputs sized to powers of 2.

Page 11: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Inherent Rules of Discrete Fourier Transforms1. The entirety of the span of a FT is equivalent to the

inverse of the sampling rate, centred if necessary.

2. Said sampling rate also determines normalisation of the dependent axis, due to nature of the summation in DFTs

3. Always sample by a power of 2

4. It is recommended the final value of an even function be dropped when performing DFTs (cyclic nature)

Rules of DFTs

Page 12: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

1. The entirety of the span of a FT is equivalent to the inverse of the sampling rate, centred if necessary. Inverse is true.

𝐸𝑛𝑡𝑖𝑟𝑒𝑡𝑦 𝑜𝑓 𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 𝑆𝑝𝑒𝑐𝑡𝑟𝑢𝑚 =1

∆𝑥

∆ξ =1

𝐴𝐿𝐿 𝑂𝐹 𝑇𝐻𝐸 𝑁𝑂𝑅𝑀𝐴𝐿 𝑆𝑃𝐴𝐶𝐸

Rules of DFTs (Cont’d)

Page 13: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

2. Said sampling rate also determines normalisation of the dependent axis, due to the nature of the summation in DFTs

𝑁𝑜𝑟𝑚𝑎𝑙𝑖𝑠𝑎𝑡𝑖𝑜𝑛 𝐹𝑎𝑐𝑡𝑜𝑟 = ∆𝑥

Rules of DFTs (Cont’d)

Page 14: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

3. Always sample by a power of 2• MATLAB adores symmetry and improves calculation speed

for large arrays

• MATLAB also has crazy, mysterious, trademarked, copyrighted, patented, and highly lucrative optimisation techniques

• Guideline for efficiency and good form

Rules of DFTs (Cont’d)

Page 15: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

Rules of DFTs (Cont’d)

• Effects of Optimisation Apparent with tic/toc

Page 16: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

4. It is recommended the final value of an even function be dropped when performing DFTs (cyclic nature)• Final point is unnecessary and potentially problematic in

repeating signals

Rules of DFTs (Cont’d)

Page 17: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Plot a Gaussian Function

𝑥 = 𝑙𝑖𝑛𝑠𝑝𝑎𝑐𝑒 −15,15,257 ;

𝑥 = 𝑥(1: 256);

𝑦 = exp −𝑥2

2;

DFTs in MATLAB

Page 18: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

DFTs in MATLAB (Cont’d)

-15 -10 -5 0 5 10 150

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1Input Function

Page 19: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Use the fft Function, Plot Reals

𝑌 = 𝑓𝑓𝑡 𝑦 ;

𝑝𝑙𝑜𝑡 𝑟𝑒𝑎𝑙 𝑌 ;

DFTs in MATLAB (Cont’d)

Page 20: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

DFTs in MATLAB (Cont’d)

0 50 100 150 200 250 300-25

-20

-15

-10

-5

0

5

10

15

20

25

Page 21: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

DFTs in MATLAB (Cont’d)

• Previous Figure Obviously Incorrect• Fourier transform of a Gaussian function is always another

Gaussian…which it was obviously not.

• Strange figure is because of the DC bias of the fft function

• We must place zero frequency in middle

Page 22: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

Frequency Shifting

• Shifting Frequencies• fftshift (A)

• Places zero frequency between negative and positive frequencies, essentially “flipping” the result

Page 23: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

Frequency Shifting (Cont’d)

• Use Function on Previous Result, Plot Reals

𝑌1 = 𝑓𝑓𝑡𝑠ℎ𝑖𝑓𝑡 𝑌 ;

𝑝𝑙𝑜𝑡 𝑟𝑒𝑎𝑙 𝑌1 ;

Page 24: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

Frequency Shifting (Cont’d)

0 50 100 150 200 250 300-25

-20

-15

-10

-5

0

5

10

15

20

25

Page 25: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Still Horribly Wrong• Oscillating amplitudes indicative of phase issues risen from

input Gaussian before fft

• Can be fixed by applying fftshift to the input as well

• Allows for function to be represented properly in time, which is the preferred input of the fft function, fixing phase issues

Frequency Shifting (Cont’d)

Page 26: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Apply fftshift to Original Gaussian

𝑦1 = 𝑓𝑓𝑡𝑠ℎ𝑖𝑓𝑡 𝑦 ;𝑝𝑙𝑜𝑡 𝑦1 ;

• Now apply fft, Shift to Centre

𝑌2 = 𝑓𝑓𝑡𝑠ℎ𝑖𝑓𝑡 𝑓𝑓𝑡(𝑦1 ;𝑝𝑙𝑜𝑡 𝑟𝑒𝑎𝑙(𝑌2) ;

Frequency Shifting (Cont’d)

Page 27: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

Frequency Shifting (Cont’d)

0 50 100 150 200 250 3000

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Page 28: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

Frequency Shifting (Cont’d)

0 50 100 150 200 250 300-5

0

5

10

15

20

25

We know that the FT of a Gaussian must be a scaled Gaussian, but what does this plot mean?

Page 29: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Axes Currently Convey no Useful Information• x-axis indicates relative position of value plotted

• y-axis requires normalisation

• We must understand the nature of normal and frequency space

Scaling

Page 30: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Applying Scaling to Result Results in Proper Results• The correct way to do an fft

𝑥 = 𝑙𝑖𝑛𝑠𝑝𝑎𝑐𝑒 −15,15,257 ;

x = x(1:256);

𝑑𝑒𝑙𝑥 = 𝑥 2 − 𝑥 1 ;

𝑓𝑟𝑒𝑞 = 𝑙𝑖𝑛𝑠𝑝𝑎𝑐𝑒 (−1/ 2 ∗ 𝑑𝑒𝑙𝑥 , 1/(2 ∗ 𝑑𝑒𝑙𝑥),257);𝑓𝑟𝑒𝑞 = 𝑓𝑟𝑒𝑞 1: 256 ;

𝑦1 = 𝑓𝑓𝑡𝑠ℎ𝑖𝑓𝑡 𝑦 ;

𝑌2 = 𝑓𝑓𝑡𝑠ℎ𝑖𝑓𝑡 𝑓𝑓𝑡(𝑦1 ∗ 𝑑𝑒𝑙𝑥;𝑝𝑙𝑜𝑡 𝑓𝑟𝑒𝑞, 𝑟𝑒𝑎𝑙(𝑌2) ;

Scaling (Cont’d)

Page 31: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

Rules of DFTs (Cont’d)

-5 -4 -3 -2 -1 0 1 2 3 4 5-0.5

0

0.5

1

1.5

2

2.5

3Proper Scaled Function

Frequency

Am

plit

ude

Page 32: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Occurs Near Sharp Edge in Fourier Space• “Jump” apparent in Fourier transform

• As number of samples increase, “jump” does not go away, merely approaches infinitely closer to the discontinuity

• Always hovers at 1.09 magnitude of max

• Occurs as the DFT is carried out with a limited number of frequencies of sines and cosines, which are incapable of representing the discontinuity

Gibbs Phenomenon

Page 33: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

Gibbs Phenomenon (Cont’d)

-10 -8 -6 -4 -2 0 2 4 6 8 10-0.4

-0.2

0

0.2

0.4

0.6

0.8

1Input Sinc Function

Page 34: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

Gibbs Phenomenon (Cont’d)

-8 -6 -4 -2 0 2 4 6 8-0.2

0

0.2

0.4

0.6

0.8

1

1.2Output Transform of Sinc Function

Page 35: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Sampling Frequency of DFT > NyquistFrequency• Nyquist frequency = 2x Sampling Frequency of Function

• Nyquist frequency is regarded as minimum sampling frequency required to reconstruct a function

• If sampling frequency of DFT is less than ideal, an identical function of lower frequency may be observed

Aliasing

Page 36: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Demonstration• Cosine was sampled and fitted with a curve fit to give

better appearance (do this in radiometry or any class with Barrett or Dubin and burn like the unwashed heathen you are)

• Frequency of the cosine function was then increased, but sampling for the DFT was not (this is dangerous in optics)

• Perceived frequency in frequency space was then plotted

Aliasing (Cont’d)

Page 37: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

Aliasing (Cont’d)

-8 -6 -4 -2 0 2 4 6 8-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1Cosine

Sample Points

Curve Fit

Actual Function

Page 38: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

Aliasing (Cont’d)

-6 -4 -2 0 2 4 6-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1Cosine

Sample Points

Curve Fit

Actual Function

Page 39: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

Aliasing (Cont’d)

-25 -20 -15 -10 -5 0 5 10 15 20 25-1

0

1

2

3

4

5

6

7DFT at Various Frequencies

10

33

50

100

200

300

500

Page 40: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Low-Pass Filter Zero Higher Frequencies

• High-Pass Filter Zero Lower Frequencies

• Reconstruct to See Results: Inverse DFT

• Use “help” Command in MATLAB to Understand “interp” and “interp2” or learn to zero-pad for final section of lab (padarray)

Advice

Page 41: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Method to Import Image into MATLAB

• Think About Effects of Filtering

• Use a Black-White Image, or Greyscale a Coloured One

• Consider the Effects of Multiple Dimensions on Scaling

Advice (Cont’d)

Page 42: Lab 7 - University of Arizona...Lab 7 Data Acquisition, Aliasing, FFT, and the Optical Fourier Transform Wente Yin February 26th, 2015 • DAQ • DFTs • DFTs in MATLAB • Frequency

• Using what you’ve learned of DFTs, explain the effects of adding a series of zeros in front of and after your function before attempting the DFT in MATLAB

Quiz