CW 2-13

ECE 3340  & generic                         Han Q. Le (c)

This part has pre-class work that you must do at home prior to the class

1. Pre-classwork - Numerical Fourier synthesis

Obtain the Fourier synthesis output of various set of coefficients given below. Use parameters given.

1.1 coefficient set 1

coef12={0.4769,-0.0836,-0.0345,0.0687,-0.0292,0,0.0008,-0.0006,0.0014,0.0042,-0.0066,0.0022,0.0003,0.0001,0.0005,0.,-0.0021} ;
datarange={-0.5,0.5} ;
nchan= ?(* you have to figure it out from coef12 given above *) ;
plotstyle={Thickness[0.02], Hue[0.,1,0.5]} ;
filling= {1-> {0,Hue[0.15,1,1,1]}} ;
axes=False;

AspectRatio should be Automatic

1.2 coefficient set 2

coef13={0.5118,-0.0237,-0.2368,0.1724,-0.0215,0.0301,-0.0352,-0.0098,0.006,0.0036,0.014,-0.0092,-0.0008,-0.0068,0.0025,0.0055,-0.0009} ;
sumall13=coef13. fcosn;
datarange={-0.5,0.5} ;
nchan= ?(* you have to figure it out from coef12 given above *) ;
plotstyle={Thickness[0.02], Hue[0.8,1,0.5]} ;
filling=  {1-> {0,Black}} ;
axes=False;

1.3 Your own Fourier coefficients:

Do as follow: Write your Studend ID and birthdate together: for example: 765432102121997. Then:

RealDigits[765432102121997]
coef00=ReverseSort @ RealDigits[765432102121997][[1]]

Obtain the last 6 digits of your cell phone in binary form: For example, 543825  (no space)

BaseForm[543825,2]
phonebinary=RealDigits[543825,2][[1]]

Take the first 15, then convert it into positive and negative

phonebinary=Take[phonebinary,15]
phonebinary=2*phonebinary-1

Assign the sign of each number of your phone binary onto your coef00:

coef00=coef00*phonebinary

Put a zero in front and a zero in back:

AppendTo[coef00,0]
PrependTo[coef00,0]

Then normalize it:

normalizefactor=1. (Sqrt[coef00 . coef00]) ;
coef00=(coef00/normalizefactor)

Answer this without doing any calculation: If you take each element of coef00, squared, and add all together, what value do you expect? In other words, what is:
                                  ((Subscript[c, 1])^2)+((Subscript[c, 1])^2)+((Subscript[c, 3])^2)+... ?

1.4 Show the image of your Fourier synthesis from coefficients coef00

You can  choose any graphical style you preferred. Choose AspectRatio to be ~ 0.5 or your choice. For the example above (delete it and replace your own). This one chooses filling option to be {1-> {Bottom,...}. But it is your choice about styling.

2. Pre-classwork - Numerical Fourier synthesis - demo

Execute each of the following to obtain results, write a paragraph of your best understanding of what you just do RIGHT UNDERNEATH EACH BLOCK after you execute it. Use Text Cell type (Alt+7 in Windows of . Do not type in an Input cell of any title type. Some blocks may take up to 10 - 15 secs to run. Wait until a calculation finishes.

2.1 House

house[u_,σx_]:=Block[{x}
,x=u (* +0.125 σx  *);
0.3(UnitBox[(x/(4.σx))]+If[x<-2 σx,0.,If[x<0.,(((x+2.σx))/(2 σx)),If[x<2. σx,(((2. σx-x))/(2 σx)) ,0] ]]+If[-1.6σx<x<-1.1 σx,0.65-0.5(((x+1.7σx))/(σx)),0.]
+If[2 σx≤ x≤ 2.5 σx,1,0])];

imghouse=Plot[house[x,0.15],{x,-0.5,0.5}
,PlotStyle->{Thick,Brown}
,Filling->{1-> {0,Hue[0.45,0.4,1]}}
,AspectRatio->Automatic]

nchan=32; funcname=house;
coef0=Table[Quiet[NIntegrate[funcname[x,0.15]*(E^(-I 2 π n x)),{x,-0.5,0.5},AccuracyGoal->3 ]]
,{n,0,nchan}] ;
coef=Join[{1},ConstantArray[2,nchan]]*coef0;
xpltarr=Range[-0.5,0.5,1/256.];
fexpn=Table[(E^(I 2 π n xpltarr )),{n,0,nchan}];
out=Re[coef . fexpn ];
imghouseFourier=ListLinePlot[out,DataRange-> {-0.5,0.5},PlotRange-> All
,PlotStyle->{Thick,Brown}
,Filling->{1-> {0,Hue[0.45,0.4,1]}}
,AspectRatio->Automatic]

Grid[{{imghouse,imghouseFourier}}]

2.2 A function is defined

fouriergraphics[{funcArtnm_,σ_,styleparm_}, nFouriercomp_,nintv_]:=Block[{coef0
,coef,xpltarr,fexpn,outFS}
,coef0=Table[Quiet[NIntegrate[funcArtnm[x,σ]*(E^(-I 2 π n x)),{x,-0.5,0.5},AccuracyGoal->3 ]]
,{n,0,nFouriercomp}] ;
coef=Join[{1},ConstantArray[2,nFouriercomp]]*coef0;
xpltarr=Range[-0.5,0.5,1./nintv];
fexpn=Table[(E^(I 2 π n xpltarr )),{n,0,nFouriercomp}];
outFS=Re[coef . fexpn ];
ListLinePlot[outFS,DataRange-> {-0.5,0.5},PlotRange-> All
,PlotStyle->styleparm[[1]]
,Filling->styleparm[[2]] 
,AspectRatio->Automatic
]
];

test the function out on House

nchan=64;nintv=256;funcname=house;  σ=0.15;
styleparm={{Thick,Brown},{1-> {0,Hue[0.45,0.4,1]}}};
imghouseFourier=fouriergraphics[{funcname,σ, styleparm},nchan,nintv]

Grid[{Show[#,ImageSize->250] & /@ { imghouse,imghouseFourier}}]

2.3 Test Fourier synthesis function with a new function

castle[x_,σ_]:=Module[{u}
,u=Abs[x];
If[Abs[u-0.3]≤ 0.08
,0.65(1-2.5Abs[u-0.3])
,If[u≤ 0.22,0.65*(0.5+0.05*Sign[Cos[24 π u]])
,0]]
];

imgoriginal=Plot[castle[x,0.15],{x,-0.5,0.5}
,PlotStyle->{Thick,Hue[0.13,1,0.4]}
,Filling->{1-> {0,Hue[0.13,1,1]}}
,AspectRatio->Automatic]

nchan=64;nintv=256;funcname=castle;  σ=0.15;
styleparm={{Thick,Hue[0.13,1,0.4]},{1-> {0,Hue[0.13,1,1]}}};
imgFourier=fouriergraphics[{funcname,σ, styleparm},nchan,nintv]

Grid[{Show[#,ImageSize->250] & /@ { imgoriginal,imgFourier}}]

3. Pre-classwork - Multivalue-functions

Execute each of the following to obtain results, write a paragraph of your best understanding of what you just do RIGHT UNDERNEATH EACH BLOCK after you execute it. Use Text Cell type (Alt+7 in Windows or similar in Mac) . Do not type in an Input cell of any Title celltype. Some blocks may take up to minute to run. Wait until a calculation finishes.

3.1 Pine tree

tri[x_,{w_,h_,s_}]:=If[Abs[x]<0.5w,h+If[x<0.,s (x+0.5w),s (0.5 w-x)],h];

pinetree[x_,σx_]:=0.5*{tri[x,{0.15,0.,40}]
,tri[x,{1,1,3}],1 (* UnitBox[x] *)
,tri[x,{0.85,2.75-0.85,3}],-2*0.5*0.85+2.75
,tri[x,{0.65,3.5-0.65,3.}],3.5-2*0.5*0.65
} ;
pinefill= {1-> {Axis,Hue[0.12,1,0.4]},
2->{{3},{None,Hue[0.37,1,0.55]}}
,4-> {{5},{None,Hue[0.33,1,0.65]}}
,6-> {{7},{None,Hue[0.27,1,0.75]}}
} ;

funcname=pinetree; funcfill=pinefill;
Plot[Release @ funcname[x,σ],{x,-0.5,0.5}
,Filling->funcfill,AspectRatio->Automatic]

nchan=32;funcname=pinetree; funcfill=pinefill;
coef0=Chop[Table[Quiet[NIntegrate[funcname[x,σ]*(E^(-I 2 π n x)),{x,-0.5,0.5}
,AccuracyGoal->3 ]]
,{n,0,nchan}],(10^(-5)) ];
coef=Join[{1},ConstantArray[2,nchan]]* # & /@ Transpose[coef0];
xpltarr=Range[-0.5,0.5,1/256.];
fexpn=Table[(E^(I 2 π n xpltarr )),{n,0,nchan}];
fouriersynth=Re[coef . fexpn ];

fouriersynthImage=ListLinePlot[fouriersynth,DataRange-> {-0.5,0.5},PlotRange-> All
,PlotStyle->{{Thickness[0.],Hue[0.35,0.3,1]}}
,Filling->funcfill
,AspectRatio->Automatic
]

3.2 Define Fourier synthesis function - version 2

For a multi-value function, a new version is needed.

fouriergraphics2[{funcArtnm_,σ_,styleparm_}, nFouriercomp_,nintv_]:=Block[{coef0
,coef,xpltarr,fexpn,outFS}
,
coef0=Chop[Table[Quiet[NIntegrate[funcArtnm[x,σ]*(E^(-I 2 π n x)),{x,-0.5,0.5}
,AccuracyGoal->3 ]]
,{n,0,nFouriercomp}],(10^(-4)) ];
If[Length[Dimensions[coef0]]≥ 2 && Depth[coef0]≥   3
,coef=Join[{1},ConstantArray[2,nFouriercomp]]* # & /@ Transpose[coef0]
,coef=Join[{1},ConstantArray[2,nFouriercomp]]*coef0;
];
xpltarr=Range[-0.5,0.5,1./nintv];
fexpn=Table[(E^(I 2 π n xpltarr )),{n,0,nFouriercomp}];
outFS=Re[coef . fexpn ] ;
ListLinePlot[outFS,DataRange-> {-0.5,0.5},PlotRange-> All
,PlotStyle->styleparm[[1]]
,Filling->styleparm[[2]]
,AspectRatio->Automatic
]
];

3.3 Test on multivalue-function: penguin

ellipse[x_,{a0_,b_,w_,h_},ibranch_]:=
If[ibranch>0.,
If[Abs[x-w]<a0,h+((b (Sqrt[(a0^2)-(w^2)+2 w x-(x^2)]))/a0) ,h]
,If[Abs[x-w]<a0,h-((b (Sqrt[(a0^2)-(w^2)+2 w x-(x^2)]))/a0) ,h]]; 
ellipseS[x_,{a0_,b_,w_,h_},ibranch_]:=
If[ibranch>0.,
If[w-a0<Abs[x]<w+a0,h+((b (Sqrt[Abs[2 w x]+(a0^2)-(w^2)-(x^2)]))/a0) ,h]
,If[w-a0<Abs[x]<w+a0,h-((b (Sqrt[Abs[2 w x]+(a0^2)-(w^2)-(x^2)]))/a0) ,h]];  

penguin[x_,σx_]:=0.3*{
ellipse[x,{0.5,2.,0,1},1]
,ellipse[x,{0.5,1.,0,1},1]
,
ellipse[x,{0.4,2,0,2},1]
 ,ellipse[x,{0.4,2,0,2},-1]  

 ,ellipse[x,{0.3,1.5,0,0.725},1]
 ,ellipseS[x,{0.1,0.5,0.15,2.7},1] 
 ,ellipseS[x,{0.1,0.5,0.15,2.7},-1] 
 ,ellipseS[x,{0.05,0.2,0.14,2.7},1] 
 ,ellipseS[x,{0.05,0.2,0.14,2.7},-1] 

,ellipseS[x,{0.15,0.4,0.2,0.},1] 

,ellipse[x,{0.125,0.25,0,2},1]
,If[Abs[x]<0.125,0.74+((0.8 (Sqrt[(0.1^2)+(x^2)]))/0.1),2.]
,2.
,0.
 } ;
penguinfill= {1-> {{2},Black}
,3-> {{13},Black}
,4-> {{13},Black}
 ,5-> {{14},White}
,6-> {{7},White},8-> {{9},Black}
,10-> {{14}, Hue[0.12,1,1]}
,11-> {{12}, Hue[0.12,1,1]}
} ;

Warning: allocate 30 secs or even minutes (depending on the CPU speed) for the execution of the block below.

start=Date[];
nchan=48;nintv=256; funcname=penguin; funcfill=penguinfill;
 plotstyle={{Thickness[0.],GrayLevel[0.5]}};
styleparm={plotstyle,funcfill};
imgoriginal=Plot[Release @ funcname[x,0.15],{x,-0.5,0.5}
,PlotStyle->plotstyle
,Filling->funcfill
,AspectRatio->Automatic];
imgFourier=fouriergraphics2[{funcname,σ, styleparm},nchan,nintv];
Grid[{Show[#,ImageSize->250] & /@ { imgoriginal,imgFourier}}]
Grid[{{"time taken: ",Take[Date[]-start,-3] . {3600,60,1} ," sec."}}, BaseStyle->{18, Italic, FontFamily->"Calibri"}]