Performing Fourier Transforms In Mathematica

2y ago
42 Views
3 Downloads
219.18 KB
7 Pages
Last View : 7d ago
Last Download : 1m ago
Upload by : Joanna Keil
Transcription

Justin Youngyoung.1022@osu.eduThe Ohio State UniversityDepartment of PhysicsPerforming Fourier Transforms in MathematicaMathematica is one of many numerical software packages that offerssupport for Fast Fourier Transform algorithms. You can performmanipulations with discrete data that you have collected in the laboratory, aswell as with continuous, analytical functions. This tutorial introduces some ofthe common functions used to perform these calculations as well as somebasic concepts and caveats that are specific to Mathematica.Load data file into an arrayIt is often the case that you have a text file that contains a number of discrete data points that youhave collected and need to analyze. This data file may be a comma-, space-, or tab- delimited(.csv) file, text file (.txt), or any other file supported by Mathematica. (Run the " ImportFormats"command to see all supported data types.) In this tutorial, we will use the "Import" command toimport an existing text file that contains some electric field data into an array (Mathematica callsarrays 'lists'). You will need to specify the path to your data file.Important! If your data is in a non-standard format, you might need to modify the way this notebookimports your data. The if statement below checks how the data file is structured and modifies thearray appropriately in order for the rest of this notebook to work. This code assumes you have adata file with a separate row for each data point. It can handle data points specified as real fieldvalues at each time, in two columns, as well as data that has been separated into real andimaginary components at each time, in three columns. If your data file is structured differently, youwill have to modify how the data arrays are being referenced throughout the rest of the notebook.This is not difficult--if you have an array called myArray, Mathematica references elements of thearray as myArray[[4]], for the 4th entry, for example (it does NOT use a 0-based index). For multidimensional arrays, you can reference elements using myArray[[4]][[2]], in order to get the 2ndelement of the 4th element of the array, for example.data Import@"sample.txt", "Table"D;n Length@dataD;If@Length@data@@1DDD 3,data Table@8data@@iDD@@1DD, data@@iDD@@2DD ä * data@@iDD@@3DD , 8i, 1, n DD;

2MyTutorial.nbPlot the waveform in the time domainIt is useful to plot the data both to get an idea of what it looks like in the time domain and to makesure it loaded properly.Plot the real part of the electric field E(t) by providing an array of (t,Re(E(t))) data points andpassing it to the ListPlot function. The array is created using the Table function. (If you are familarwith basic programming structures, Table is essentially a 'for' loop.)In[139]: ListPlot@Table@8data@@iDD@@1DD, Re@data@@iDD@@2DDD , 8i, 1, n D,Joined True, PlotRange All, FrameLabel 8"t", "Re@EHtLD" , Frame TrueD1.00.8Out[139] Re@EHtLD0.60.40.20.0-0.2-10-50t510Plot the imaginary part of the electric field.In[140]: ListPlot@Table@8data@@iDD@@1DD, Im@data@@iDD@@2DDD , 8i, 1, n D,Joined True, PlotRange All, FrameLabel 8"t", "Im@EHtLD" , Frame TrueD0.0Out[140] Im@EHtLD-0.2-0.4-0.6-10-50t510

MyTutorial.nbPlot the intensity of the field.In[141]: ListPlotATableA9data@@iDD@@1DD, Abs@data@@iDD@@2DDD2 , 8i, 1, n E,Joined True, PlotRange All, FrameLabel 8"t", "Intensity" , Frame TrueE1.0Out[141] Intensity0.80.60.40.20.0-10-50t510Transform the data into the frequency domainFind the temporal spacing between data points. In this example, the spacing is the same betweenall the data points.In[142]: Out[142] Dt Hdata@@2DD@@1DD - data@@1DD@@1DDL0.1Create an array of the field amplitudes to pass to the Fourier function.In[143]: fieldValues Table@data@@iDD@@2DD, 8i, 1, n D;Compute the Fourier transform E(Ω) using the built-in function. Important! The sample data array isordered from negative times to positive times. However, Mathematica requires that the arraypassed to the Fourier function be ordered starting with the t 0 element, ascending to positive timeelements, then negative time elements. To account for this, we run the RotateLeft command on ourdata array. The array that results after taking the transform is ordered starting from zero-frequency,increasing to the maximum frequency; then it jumps to the most negative frequency, and increasesto zero. Therefore, when we want to plot the result of the transform in frequency space, we need toshift the array again after the transform. We use RotateRight (by the same amount) before plotting,so that our data is ordered from negative frequencies to positive frequencies, with the zerofrequency component in the middle.In[144]: FFTValues Fourier@RotateLeft@fieldValues, n 2 - 1D, FourierParameters 81, - 1 D;FFTValues RotateRight@FFTValues, n 2 - 1D;Find the spectral spacing (in Hz).1In[146]: DΝ n DtOut[146] 0.0390625Create a list of frequency-axis values (in angular Hz) to use in the plot of E(Ω).3

4MyTutorial.nbCreate a list of frequency-axis values (in angular Hz) to use in the plot of E(Ω).In[147]: frequencyValues Table@DΝ * Hi - n 2L, 8i, 1, n D;Plot the waveform in the frequency domainPlot the real part of E(Ν).In[148]: ListPlot@Table@8frequencyValues@@iDD, Re@FFTValues@@iDDD , 8i, 1, n D,Joined True, PlotRange All, FrameLabel 8"Ν", "Re@EHΝLD" , Frame TrueDOut[148] Re@EHΝLD1050-4-20Ν24Plot the imaginary part.In[149]: ListPlot@Table@8frequencyValues@@iDD, Im@FFTValues@@iDDD , 8i, 1, n D,Joined True, PlotRange All, FrameLabel 8"Ν", "Im@EHΝLD" , Frame TrueDOut[149] Im@EHΝLD50-5-10-4-20Ν24

MyTutorial.nbPlot the frequency power intensity spectrum.In[150]: ListPlotATableA9frequencyValues@@iDD, Abs@FFTValues@@iDDD2 , 8i, 1, n E,PlotRange All, Joined True, FrameLabel 8"Ν", "Intensity" , Frame TrueE200Out[150] Intensity150100500-4-20Ν24Plot the spectral phase of the waveform versus frequencySince complex values have an associated phase in the complex plane, we want to plot the spectralphase as a function of frequency. However, the Arg(z) function is periodic in multiples of 2Π.Therefore, we need to 'unwrap' the angles. Unfortunately, Mathematica does not provide a built-infunction to do this. Here is some short code that provides this functionality, returns the phasevalues in an array, and plots the values.In[151]: phase Arg@FFTValuesD;phase FoldList@Round@Hð1 - ð2L H2 PiLD 2 Pi ð2 &, First@phaseD, Rest@phaseDD;ListPlot@Table@8frequencyValues@@iDD, phase@@iDD , 8i, 1, n D,Joined True, PlotRange All, FrameLabel 8"Ν", "Φ" , Frame TrueD-5Φ-10Out[153] -15-20-25-4-20Ν245

6MyTutorial.nbInverse transform back into the time domainPerform a reverse Fourier transform (remembering to shift the array before the transform, since weplotted it above ordered from negative to positive values!).In[154]: originalFFTValues InverseFourier@RotateLeft@FFTValues, n 2 - 1D, FourierParameters 81, - 1 D;Plot the intensity versus time (remembering to shift the array before plotting.you should be goodat this by now)In[155]: originalPulse RotateRight@originalFFTValues, n 2 - 1D;ListPlotATableA9data@@iDD@@1DD, Abs@originalPulse@@iDDD2 , 8i, 1, n E,Joined True, PlotRange All, FrameLabel 8"t", "Intensity" , Frame TrueE1.00.8IntensityOut[156] 0.60.40.20.0-100t-5510As expected, we get the same pulse back as before, since we did not apply any transfer function toit in the frequency domain. This is a good test to see if your software is working.Working with analytic functionsYou can take the Fourier transform of an analytic function as follows .In[157]: FourierTransformAExpA- a t2 E, t, Ω, FourierParameters 81, - 1 E-ãΩ2Π4aOut[157] a. as well as its inverse-ãIn[158]: Ω2Π4aInverseFourierTransformB, Ω, t, FourierParameters 81, - 1 FaOut[158] ã-a t2You can use these methods to create discrete data sets from a given analytic expression, forexample, by sampling at specified times and frequencies.

MyTutorial.nbYou can use these methods to create discrete data sets from a given analytic expression, forexample, by sampling at specified times and frequencies.Possible IssuesHere are some common issues you might run into when using Mathematica, as well as generalthings to keep in mind when performing Fourier transforms. When working with analytic functions, be aware of the sign convention used for plane waves traveling tothe left and those traveling to the right. You are free to choose the sign convention you prefer when distinguishing between forward and reversetransforms, as long as you are consistent. Mathematica' s convention is different from that used in class,but you can force Mathematica to use a specified convention by setting the FourierParameters property inthe Fourier and InverseFourier functions. I have set my transforms to use the sign convention used inclass. See the documentation for more information. When generating sample data to work with, make sure your sample window is large enough in order toaccomodate the entire signal of interest. This may involve using more or less data points, or settingdifferent temporal and spectral spacings. It is easy to get the array rotations mixed up when preparing your array for forward and reversetransforms, and in plotting. However, it is easy to tell if your array ordering is incorrect; your data willappear to be discontinuous. Perform another rotation and try plotting it again. In order to avoid array rotation problems when you make a change in your notebook, such as in a transferfunction, it is useful to re-evaluate the entire notebook from start to end in order to properly reset yourarrays. Make sure you are using frequency values or angular frequency values, as appropriate. It is often convenient to work only with the pulse envelope. Therefore, be sure to distinguish betweeninstantaneous and carrier frequencies in your calculations. Check the scale of your units to be sure everything matches up! (e.g. Hz vs. THz)SummaryWith these tools, you should now be able to perform forward and reverse Fourier transforms ofdiscrete and continuous data sets in Mathematica. You have seen that the Table function is one ofthe most useful functions in Mathematica to create and manipulate arrays. You have also seensome of the ways that you can visualize your data, by way of examining the magnitudes andphases of complex-valued fields, as well as their frequency spectra. The best way to learn aboutMathematica's capabilities is to simply start using the software and try as many examples aspossible. The documentation is quite helpful when you need a description of what certain functionsdo, what their assumptions are, and what parameters they can take. Most manual pages give youexamples to work with that you can modify for your own purposes. The web is also a greatresource for finding examples. Have fun transfoming, please email me if you find any errors in thisdocument, and remember, transforms are just robots in disguise!7

Performing Fourier Transforms in Mathematica Mathematica is one of many numerical software packages that offers support for Fast Fourier Transform algorithms. You can perform manipulations with discrete data that you have collected in the laboratory, as well as with continuous, analytical functions. This tutorial introduces some of

Related Documents:

1 Mathematica Basics This chapter is an introduction to Mathematica.We briefly describe many of the most important and basic elements of Mathematica and discuss a few of the more common technical issues related to using Mathematica.Since our primary goal is to use Mathematica to help us understand calculus, you should not initially spend a great amount of time pouring

Introduction.NET/Link Welcome to .NET/Link, a product that integrates Mathematica and Microsoft's .NET platform.NET/Link lets you call .NET from Mathematica in a completely transparent way, and allows you to use and control the Mathematica kernel from a .NET program. For Mathematica users,.NET/Link makes the entire .NET world an automatic extension to the Mathematica environ-

Dec 09, 2005 · Beginner’s Mathematica Tutorial Introduction This document is designed to act as a tutorial for an individual who has had no prior experience with Mathematica. For a more advanced tutorial, walk through the Mathematica built in tutorial located at Help Tutorial on the Mathematica Task Bar.

Online Help from Mathematica 2 Mathematica Tutorial.nb. Introduction Mathematica is a system for doing mathematics on the computer.It can do numerics,symbolics,graphics and is also a programming language.Mathematica has infinite precision.It can plot functions of a single variable; make

mathematica Remarks This section provides an overview of what wolfram-mathematica is, and why a developer might want to use it. It should also mention any large subjects within wolfram-mathematica, and link out to the related topics. Since the Documentation for wolfram-mathematica is new, you may need to create initial versions of those related .

PROGRAMMING IN MATHEMATICA, A PROBLEM-CENTRED APPROACH 7 1.3. Algebraic computations. One of the abilities of Mathematica is to handle symbolic com-putations. Consider the expression (x 1)2. One can use Mathematica to expand this expression: Expand[(x 1) 2] 1 2x x2 Mathematica can also do the inverse of this task, namely to factorize an expression:

Start up Mathematica from a Linux desktop terminal window. Parallel Mathematica jobs can be submitted from with the Mathematica notebook interface as well as using PBS command files and the example scripts show how to setup and submit the jobs Documentation:Submitting Mathematica Parallel Jobs (UVACSE) October 8, 2014 44 / 46

The Certificate in Russian Language is six- month programme of 16 Credits. The programme aims at providing beginners with basics of Russian Language. The objective of the programme is to introduce learners to the basics of Russian grammar and phonetics so that they can read, write, listen and speak Russian in an accurate manner. The programme is bilingual (Russian/English) in medium and has .