Mathematica:
Electric Engineering Workshop

UFRJ - Brazil - 2013

by Daniel de Souza Carvalho

Twitter : @danielscarvalho
E-Mail: daniel.carvalho@advisor.net.br

Why Mathematica

Concentrate your efforts in the research or projects!!! 

Do not waste time dealing with memory management, creating data structures, objects, pointers, read and write diverse file formats! Do not waste time with nested FOR loops!!

Mathematica can do the dirt job for you!!

Moreover, Mathematica provides an huge set of mathematical functions and algorithms tested and ready to use, and:

Sophisticated Data Visualization and Interaction

Symbolic and Numerical Computation

Signal and Image Processing

Parallel Processing (take the advantage of your computers)

Rich documentation, examples and community

Try, test, experiment, prototype!! Have fun!

Topics

Graphics

Fourier

Convolution

Image Processing

Getting Data From Devices (Arduino)

Parallel Processing, Compilation, CUDA

Symbolics (Integrating and Derivation)

Numerical Processing

Signals

Manipulate

Mathematica basic concepts!!!

Functional Programming

List Manipulation

Learning Mathematica

“Example isn’t another way to teach, it is the only way to teach.” - Albert Einstein

Mathematica support different programming paradigms.
To get the better from Mathematica learn the basics:

	Functional Programming (Press F1 for help and tutorials)
		Article (PDF): Why Functional Programming Matters
	List Manipulation

The Wolfram Solution for: Electrical Engineering

Demonstrations for: Electrical Engineering

Mathematica built-in function: More than 3.000 at version 9

 

Do more with less code: 
http://blog.wolfram.com/2012/11/14/code-length-measured-in-14-languages/


References:

	Mathematica Books
	Tutorial Collection
	Mathematica Book by S. Wolfram (PDF) 2003
	On-line training
	Math World
	The Wolfram Function Site

Mathematica Basics

Concepts

List Manipulation

Functional Programming

Syntax

α+β

FullForm[α+β]

FullForm[a+b/c]

Map[f,{x,y,z}]

Map[f,Expand[(x+2)^5]]

Table[x,{x,1,10,2}]

Table[1/x,{x,1,10,1}]

Table[10 i+j,{i,3},{j,3}]

MatrixForm[%]

10/3

N[10/3]

N[10/3,100]

Pi

N[Pi,10]

N[Pi,100]

Prime[30]

Abs[-10]

Mod[3,2]

Sin[Pi]

Cos[2 Pi]

Rule[a,1]

ReplaceAll[{a,b},{a-> 1, b-> 2}]

Flatten[{{i,j,k},{x,y,z}}]

Map[f,{x,y,z}]

Partition[{1,2,a,b,Π,α,β},2]

Sqrt[2]

Sqrt[4]

Dimensions[{{1,2,3,4},
{a,b,c,d}}]

TraditionalForm[  2/
(x Pi /( -(3 I) 
^(Sqrt[x + E])) )]

resultado = Sqrt[3]/2;

resultado

MyFunc[a_]:=Abs[ Sqrt[a]/Cos[a]];

MyFunc[8]

TraditionalForm[MyFunc[x]]

Table[MyFunc[x],{x,0,10,1}]

RuleIt[dataList_]:=Rule@@@Partition[Riffle[Drop[#,-1],Drop[#,1]],2]& [dataList]

Ploting Data

Plot[Sinc[x],{x,-10,10}]

ListLinePlot[Table[Sinc[x],{x,-10,10, .1}],AxesOrigin->{100,0}]

sinc2D=Table[Sinc[x]+Sinc[y],{x,-10,10, .1},{y,-10,10,.1}]

Dimensions[sinc2D]

ArrayPlot[sinc2D,ColorFunction-> Hue]

ListDensityPlot[sinc2D,ColorFunction-> Hue]

ListContourPlot[sinc2D,ColorFunction-> Hue]

ListPlot3D[sinc2D,ColorFunction-> "DarkRainbow"]

ArrayPlot[Log[Abs[Fourier[sinc2D]]],ColorFunction-> "Rainbow"]

Manipulate[ArrayPlot[data=Table[Sin[x*f]*Cos[y/f],{x,-size,size,.1},{y,-size,size,.1}],ColorFunction-> "TemperatureMap",PlotLabel-> Dimensions[data]],
{size,2,20,1},
{f,1,10}]

ArrayPlot[Log[Abs[Fourier[data]]],ColorFunction-> "Rainbow"]

Image Processing

bandeira=;

ImageChannels[bandeira]
Dimensions[ImageData[bandeira]]
ImageDimensions[bandeira]

ImageHistogram[bandeira,Appearance->"Separated"]

ColorSeparate[bandeira]

ColorConvert[bandeira,"Grayscale"]

ArrayPlot[Abs[Log[Fourier[ImageData[ColorConvert[bandeira,"Grayscale"]]]]]]

Blur[ColorConvert[bandeira,"Grayscale"],30]

EdgeDetect[bandeira]

Manipulate[
Graphics[
Raster[If[original,#,
Sqrt[ListConvolve[
{{1,2,1},
{0, 0, 0},
{-1,-2,-1}},#]^2 +
ListConvolve[
{{1,0,-1},
{2, 0, -2},
{1,0,-1}},#]^2]]&[Reverse[Take[ExampleData[{"TestImage",image},"GrayLevels"],{1,-1,sample},{1,-1,sample}]]]],ImageSize-> 400],
{{sample,2,"image sample"},1,7,1},
{{image,"Airplane"},Sort[#[[2]]&/@ExampleData["TestImage"]]},
{{original,False,"original image"},{True,False}},SynchronousUpdating->False]

Source: http://demonstrations.wolfram.com/SobelEdgeDetectionFilter/

Signal Processing

Convolve

signal1=Table[Sqrt[x]+RandomReal[]*Sin[x],{x,30}]
ListLinePlot[signal1, PlotStyle-> Thick]
ListLinePlot[ListConvolve[{1,1,2,1,1}/6,signal1],PlotStyle-> Thick]

WaveletScalogram[DiscreteWaveletTransform[signal1,Automatic,2]]

signal2=Table[Sinc[x*10],{x,-10,10,.1}];
ListLinePlot[signal2,PlotRange-> All,PlotStyle-> Thick]
WaveletScalogram[DiscreteWaveletTransform[signal2,Automatic,3],ColorFunction->Hue]

WaveletScalogram[ContinuousWaveletTransform[signal2]]

signal3= Table[Sin[x]*Cos[x]*x^2,{x,-12,12,.1}];
ListPlot[signal3]

WaveletScalogram[ContinuousWaveletTransform[signal3]]

signal4=Table[Cos[x^2]+RandomReal[],{x,0,2Pi,0.01}];

ListPlot[signal4]

ListLinePlot[signal4]

Manipulate[ListLinePlot[LowpassFilter[signal4,f]],{f,0.1,1,.1}]

FExp[x_] := Exp[-Abs[x]];
FTwo[x_] := 2;
Rect[x_] :=Piecewise[{{0,-3> x },{0, x > 3}},2];
FShift [a_] :=RotateLeft[a,Floor[Length[a]/2]];

Manipulate[
data=Table[N[function[ n]+(RandomInteger[9]*noise)],{n,-5*range,5*range}];
Grid[{{plot[data,PlotRange->All,AxesLabel-> {t,g[t]},PlotStyle->Directive[Thick,Red],PlotLabel-> "discrete signal",ImageSize->{210,210},ImagePadding->{{25,25},{10,35}},InterpolationOrder-> interpolation,Axes-> axes,Filling-> If[plot===ListPlot,Axis,None], DataRange->{-range,range}],plot[FShift[Re[Fourier[data]]],PlotRange->All,AxesLabel-> {ω,G[ω]},PlotStyle->Directive[Thick,Blue],PlotLabel -> "real Fourier plane",ImageSize->{210,210},ImagePadding->{{25,25},{10,35}},InterpolationOrder-> interpolation,Axes -> axes,Filling-> If[plot===ListPlot,Axis,None],DataRange->{-range,range}]},
{plot[FShift[Im[Fourier[data]]],PlotRange->All,AxesLabel-> {ω,G[ω]},PlotStyle->Directive[Thick,Orange],PlotLabel-> "imaginary Fourier plane",ImageSize->{210,210},ImagePadding->{{25,25},{10,35}},InterpolationOrder-> interpolation,Axes -> axes,Filling-> If[plot===ListPlot,Axis,None],DataRange->{-range,range}],
plot[FShift[Abs[Fourier[data]]],PlotRange->All,AxesLabel-> {ω,G[ω]},PlotStyle->Directive[Thick,Green],PlotLabel -> "absolute Fourier plane",ImageSize->{210,210},ImagePadding->{{25,25},{10,35}},InterpolationOrder-> interpolation,Axes -> axes,Filling-> If[plot===ListPlot,Axis,None],DataRange->{-range,range}]}},Frame-> All],
{range,4,20,1,ImageSize->Tiny},
{{noise,0,"white noise"},0,.1,.01,ImageSize->Tiny},
{interpolation,0,3,1,ImageSize->Tiny},
{{function,Sin,"signal"},{Sin -> "sine",Sinc -> "sinc",FExp -> TraditionalForm@Exp[-Abs[t]],FTwo -> "constant",UnitStep ->"unit step",Sign -> "signum",Rect -> "rect"}},
{{plot,ListLinePlot,"plot style"},
{ListLinePlot -> "line",ListPlot-> "points"}},
{axes,{True,False}},SaveDefinitions-> True,TrackedSymbols:>Manipulate,ControlPlacement->Left]

Symbolic and Numerical Computation

From M101 Wolfram Official Training

Hands on!!

Parallel Processing, CUDA and Compilation

f1=Compile[{{x,_Real}},Abs[1-Sqrt[x]]]

f1[Pi]

ParallelTable[Prime[(x+y)*10000],{x,10},{y,10}]

Parallelize[Table[(1/(Sin[x]*Cos[y]))*Exp[x*y]^2,{x,10,10,.1},{y,10,10,.1}]]

CUDA - GPU Computing

Arduino Mathematica Interaction

Needs["NETLink`"]

InstallNET[];

(* NETTypeInfo["System.IO.Ports.SerialPort"] *)

serialPort=NETNew["System.IO.Ports.SerialPort","COM6"]

serialPort@Open[]

(* AEvent[sender_,evt_]:=(j=serialPort@ReadChar[]) 
AEvent[sender_,evt_]:=(j=serialPort@ReadByte[]) *)

AEvent[sender_,evt_]:=(data=ToExpression[serialPort@ReadLine[]])

AddEventHandler[serialPort@DataReceived,AEvent]

Dynamic[data]

dataSet ={};

AngularGauge[Dynamic[data[[1]]],{0,1100},GaugeLabels->Automatic]

Dynamic[AppendTo[dataSet,data[[2]]]]
Dynamic[ListLinePlot[dataSet]]

serialPort@Close[]

Next: ArduinoLink

Arduino and Mathematica — Computation in the Maker World
Christopher Wolfram and Keshav Saharia

In this talk from the Wolfram Technology Conference, Christopher Wolfram and Keshav Saharia explain how they’ve connected Mathematica to the Arduino and demonstrate some of the possibilities for using the connection to measure, compute, visualize, and control our physical world.

Video: http://www.wolfram.com/broadcast/video.php?channel=104&video=1377

ArduinoLink at Github: https://github.com/keshavsaharia/ArduinoLink

Example

Experimental Oscilloscope with Arduino and Mathematica 

Video: http://www.youtube.com/watch?v=REXSOtS8BFI

Reference:

http://arduino.cc/en/Tutorial/AnalogReadSerial
http://williamjturkel.net/2011/12/25/connecting-arduino-to-mathematica-on-mac-os-x-with-serialio/
http://playground.arduino.cc/Interfacing/Mathematica