(*Fourier series for f(x) = x*)

Table[2*Integrate[x Cos[2Pi x n],{x,0,1}],{n,0,5}]
Table[2*Integrate[x Sin[2Pi x n],{x,0,1}],{n,1,7}]

y[x_,min_,max_]:=1/2-1/(Pi)Sum[Sin[2n Pi x]/n,{n,min,max}]

g=Plot[{x-Quotient[x,1],y[x,1,1],y[x,1,2],y[x,1,10],1/2},{x,-1.5,1.5},PlotStyle->{{Black},{Red,Dashed},{Blue,Dashed},{Orange,Dashed},{Green,Dashed}},AspectRatio->1/3]

Export[NotebookDirectory[]<>"Fourier-f=x.png",g,"PNG",ImageResolution->200,Background->None]

Animate[Plot[{x-Quotient[x,1],y[x,1,nn]},{x,-2,2},PlotStyle->{{Black},{Red}},PlotLabel->{"Nmax=",nn},PlotRange->{-0.1,1.1}],{nn,1,21,1},AnimationRunning->False,DisplayAllSteps->True]

(*Fourier series for f(x) = Heaviside(x)*)
Table[2*Integrate[(2HeavisideTheta[x]-1) Cos[2Pi x n],{x,-1/2,1/2}],{n,0,5}]
Table[2*Integrate[(2HeavisideTheta[x]-1)Sin[2Pi x n],{x,-1/2,1/2}],{n,1,7}]

y[x_,min_,max_]:=2/(Pi)Sum[(1-(-1)^n)Sin[2n Pi x]/n,{n,min,max}]

g=Plot[{Cos[Quotient[x,1/2]Pi],y[x,1,1],y[x,1,3],y[x,1,10]},{x,-1.5,1.5},PlotStyle->{{Black},{Red,Dashed},{Blue,Dashed},{Orange,Dashed},{Green,Dashed}},AspectRatio->1/3]

Export[NotebookDirectory[]<>"Fourier-Signal.png",g,"PNG",ImageResolution->200,Background->None]

Animate[Plot[{Cos[Quotient[x,1/2]Pi],y[x,1,nn]},{x,0,1},PlotStyle->{{Black},{Red}},PlotLabel->{"Nmax=",nn}],{nn,1,21,2},AnimationRunning->False,DisplayAllSteps->True]

g=Plot[{Cos[Quotient[x,1/2]Pi],y[x,1,1],y[x,3,3],y[x,5,5],y[x,7,7],y[x,9,9]},{x,0,1}]

Export[NotebookDirectory[]<>"Fourier-Signal2.png",g,"PNG",ImageResolution->200,Background->None]

g=ParametricPlot3D[{{x,0,Cos[Quotient[x,1/2]Pi]},{x,1,y[x,1,1]},{x,3,y[x,3,3]},{x,5,y[x,5,5]},{x,7,y[x,7,7]},{x,9,y[x,9,9]},{x,11,y[x,11,11]}},{x,0,1},BoxRatios->{1,2,1},Boxed->False,PlotRange->All,ViewPoint->{4,-5,3},PlotStyle->Thick]

Export[NotebookDirectory[]<>"Fourier-Signal3.png",g,"PNG",ImageResolution->200,Background->None]

g=ListPlot[Table[{n,2/(n Pi)(1-(-1)^n)},{n,1,15}],PlotStyle->{Thick,Red},PlotRange->All]

Export[NotebookDirectory[]<>"Fourier-Signal4.png",g,"PNG",ImageResolution->200,Background->None]

(*Pulso localizado*)

Clear[a,an,bn,cn,phin]
Table[2/(a L)*Integrate[Cos[2Pi x n/L],{x,0,a},Assumptions->{L>0,0<a<L}],{n,1,5}]
Table[2/(a L)*Integrate[Sin[2Pi x n/L],{x,0,a},Assumptions->0<a<1],{n,1,5}]

an[a_,n_,L_]:=If[n==0,2,Sin[2n a Pi/L]/(a n Pi/L)]
bn[a_,n_,L_]:=If[n==0,0,2Sin[n a Pi/L]^2/(a n Pi/L)]
cn[a_,n_,L_]:=Sqrt[an[a,n,L]^2+bn[a,n,L]^2]
phin[a_,n_,L_]:=ArcTan[an[a,n,L],bn[a,n,L]]

L=10;
a=0.045;
g1=ListPlot[Table[{2Pi n/L,an[a,n,L]},{n,0,50L}],PlotStyle->{Thick,Blue},PlotRange->{Automatic,2.01}]
g2=ListPlot[Table[{2Pi n/L,bn[a,n,L]},{n,0,50L}],PlotStyle->{Thick,Red},PlotRange->{Automatic,2}]
g3=ListPlot[Table[{2Pi n/L,cn[a,n,L]},{n,0,50L}],PlotStyle->{Thick,Black},PlotRange->{Automatic,2}]
g4=ListPlot[Table[{2Pi n/L,phin[a,n,L]},{n,0,50L}],PlotStyle->{Thick,Orange},PlotRange->All]
Clear[L,a]

Export[NotebookDirectory[]<>"Fourier-Pulse1-0.045tenfold.png",g1,"PNG",ImageResolution->200,Background->None]
Export[NotebookDirectory[]<>"Fourier-Pulse2-0.045tenfold.png",g2,"PNG",ImageResolution->200,Background->None]
Export[NotebookDirectory[]<>"Fourier-Pulse3-0.045tenfold.png",g3,"PNG",ImageResolution->200,Background->None]
Export[NotebookDirectory[]<>"Fourier-Pulse4-0.045tenfold.png",g4,"PNG",ImageResolution->200,Background->None]

Animate[{ListPlot[Table[{2Pi n/L,cn[a,n,L]},{n,1,20L}],PlotStyle->{Thick,Black},PlotRange->{0,2},PlotLabel->{"a=",a},AxesLabel->{k_n,c_n}],ListPlot[Table[{2Pi n/L,phin[a,n,L]},{n,1,20L}],PlotStyle->{Thick,Green},PlotRange->{0,Pi},PlotLabel->{"a=",a},AxesLabel->{k_n,Phi_n}]},{L,1,10,1},{a,0.01,0.45},AnimationRunning->False,DisplayAllSteps->True]

a=0.045;
g=Plot[2Sin[k a/2]/(k a),{k,0,314},PlotStyle->Thick]
Clear[a]

Export[NotebookDirectory[]<>"Fourier-Pulse3-0.045Infinity.png",g,"PNG",ImageResolution->200,Background->None]

(*Dispersão: Corda rígida*)

(*e=0.1;
pts=Table[{t,x,NIntegrate[Exp[-k^2/2]Cos[k x-k Sqrt[1+e k^2]t],{k,-Infinity,Infinity}]},{x,-5,20,0.1},{t,0,20,0.05}];*)

(*Export[NotebookDirectory[]<>"wave-packet-dispersion-e=0.1.dat",pts,"MAT"]*)

(*e=0.3;
pts=Table[{t,x,NIntegrate[Exp[-k^2/2]Cos[k x-k Sqrt[1+e k^2]t],{k,-Infinity,Infinity}]},{x,-5,20,0.1},{t,0,20,0.05}];*)

(*Export[NotebookDirectory[]<>"wave-packet-dispersion-e=0.3.dat",pts,"MAT"]*)

pts=Import[NotebookDirectory[]<>"wave-packet-dispersion-e=0.1.dat","MAT"];
pts2=Import[NotebookDirectory[]<>"wave-packet-dispersion-e=0.3.dat","MAT"];

Animate[ListPlot[{Table[{pts[[x,t,2]],pts[[x,t,3]]},{x,1,251}],Table[{pts2[[x,t,2]],pts2[[x,t,3]]},{x,1,251}],Table[{x,Sqrt[2Pi]Exp[-(x-(t-1)/20)^2/2]},{x,-5,20,0.1}]},PlotRange->{-1,2.5},Axes->False,Joined->True,PlotStyle->{{Thick,Blue},{Thick,Red},{Dashed,Black,Thick}}],{t,1,401,10},AnimationRunning->False,DisplayAllSteps->True]

Int1=Table[Sum[Abs[pts[[x,t,3]]],{x,1,251}],{t,1,401}];
Int2=Table[Sum[Abs[pts2[[x,t,3]]],{x,1,251}],{t,1,401}];
x1=Table[Sum[pts[[x,t,2]]*Abs[pts[[x,t,3]]],{x,1,251}]/Int1[[t]],{t,1,401}];
x2=Table[Sum[pts2[[x,t,2]]*Abs[pts2[[x,t,3]]],{x,1,251}]/Int2[[t]],{t,1,401}];
xx1=Table[Sum[(pts[[x,t,2]])^2*Abs[pts[[x,t,3]]],{x,1,251}]/Int1[[t]],{t,1,401}];
xx2=Table[Sum[(pts2[[x,t,2]])^2*Abs[pts2[[x,t,3]]],{x,1,251}]/Int2[[t]],{t,1,401}];
Dx1=Table[Sqrt[xx1[[t]]-x1[[t]]^2],{t,1,401}];
Dx2=Table[Sqrt[xx2[[t]]-x2[[t]]^2],{t,1,401}];

ListPlot[{Table[{(t-1)/20,x1[[t]]},{t,1,180}],Table[{(t-1)/20,x2[[t]]},{t,1,100}]},Joined->True,PlotStyle->{{Thick,Blue},{Thick,Red}},AxesLabel->{"Tempo","Dx"}]
ListPlot[{Table[{(t-1)/20,Dx1[[t]]},{t,1,180}],Table[{(t-1)/20,Dx2[[t]]},{t,1,100}]},Joined->True,PlotStyle->{{Thick,Blue},{Thick,Red}},AxesLabel->{"Tempo","Dx"}]

ListPlot[{Table[{(t-1)/20,Int1[[t]]},{t,1,401}],Table[{(t-1)/20,Int2[[t]]},{t,1,401}]},Joined->True,PlotStyle->{{Thick,Blue},{Thick,Red}},AxesLabel->{"Tempo","Dx"}]

(*Notas musicais*)

fdo=261.6;
fre=293.7;
fmi=329.6;
ffa=349.2;
fso=392;
fla=440;
fsi=493.9;

freq={261.6,293.7,329.6,349.2,392,440,493.9}

p[t_]:=Sin[2Pi fla t];
Plot[p[t],{t,0,0.01}]
Play[p[t],{t,0,2}]

p[t_]:=Sin[2Pi fla t]-0.5Sin[3*2Pi fla t];
Plot[p[t],{t,0,0.01}]
Play[p[t],{t,0,2}]

p[t_]:=2/(Pi)Sum[(1-(-1)^n)Sin[n*2Pi fla t]/n,{n,1,15}];
Plot[p[t],{t,0,0.01}]
Play[p[t],{t,0,2}]

p[t_]:=Sin[2Pi fla t]+Sin[2Pi (20+fla) t];
Plot[p[t],{t,0,0.1}]
Play[p[t],{t,0,2}]

envelope[t_,tau_,t0_]:=If[t<t0,0,(t-t0)^2*Exp[-((t-t0)/tau)^2]]
nota[t_,tau_,t0_,n_]:=Sin[2*Pi*freq[[n]]*t]*envelope[t,tau,t0]

Plot[{nota[t,0.1,1,2],envelope[t,0.1,1]},{t,0,2},PlotRange->All]
Play[nota[t,0.1,1,2],{t,0,2}]

tau=0.25;
t1=0.5;
t2=0.1;
music[t_]:=nota[t,tau,0,1]+nota[t,tau,t1,2]+nota[t,tau,2t1,3]+nota[t,tau,3t1,4]+nota[t,tau,4t1+t2,4]+nota[t,tau,5t1,4]+nota[t,tau,6t1,1]+nota[t,tau,7t1,2]+nota[t,tau,8t1,1]+nota[t,tau,9t1,2]+nota[t,tau,10t1+t2,2]+nota[t,tau,11t1,2]+nota[t,tau,12t1,1]+nota[t,tau,13t1,5]+nota[t,tau,14t1,4]+nota[t,tau,15t1,3]+nota[t,tau,16t1+t2,3]+nota[t,tau,17t1,3]+nota[t,tau,18t1,1]+nota[t,tau,19t1,2]+nota[t,tau,20t1,3]+nota[t,tau,21t1,4]+nota[t,tau,22t1+t2,4]+nota[t,tau,23t1,4]

Plot[music[t],{t,0,24t1+tau}]

Play[music[t],{t,0,24t1+tau},PlayRange-> All]

f=40;
w=2Pi*f;
p[t_]:=Sin[w t^2.5];
Plot[p[t],{t,0,2}]
Play[p[t],{t,0,4}]

f=80;
w=2Pi*f;
p[t_]:=Sin[w t/Sin[3t]];
Plot[p[t],{t,0,2}]
Play[p[t],{t,0,4}]

Integrate[Exp[-(x-x0)^2/(2 s^2)]/(Sqrt[2Pi]s),{x,-Infinity,Infinity},Assumptions->{x0>0,s>0}]
Integrate[Exp[-(x-x0)^2/(2 s^2)-I k x]/(Sqrt[2Pi]s),{x,-Infinity,Infinity},Assumptions->{x0>0,s>0,k>0}]

ff[k_]:=(E^(-(1/2) k (k (s^2)+2 I x0)))/(((E^(-((x0^2)/(2 (s^2))))) (Sqrt[2 π]))/s)

Integrate[ff[k],{k,-Infinity,Infinity},Assumptions->{x0>0,s>0}]
Integrate[k ff[k],{k,-Infinity,Infinity},Assumptions->{x0>0,s>0}]
Integrate[k^2 ff[k],{k,-Infinity,Infinity},Assumptions->{x0>0,s>0}]

FullSimplify[Sqrt[(((s-x0) (s+x0))/(s^4))-(-((I x0)/(s^2)))^2]]

Integrate[Exp[-I k x]/a,{x,-a/2,a/2},Assumptions->{a>0,k>0}]