How to output periodogram result to a list in Mathematica? - plot

I have a time domain data. After I did discrete fourier transform with Periodogram, how can I output the plot into a list for further manipulation?
Periodogram[data[[All, 2]], SampleRate -> 3000000/0.01, GridLinesStyle -> Directive[Red, Dashed], PlotRange -> {{100000000 - 10000, 100000000 + 10000}, All}]
Thanks!

A List can be extracted from the Periodogram output, based on its InputForm, as follow. (We begin with some made-up data.)
data = Table[2 Sin[0.2 \[Pi] n ] + Sin[0.5 \[Pi] n] + RandomReal[{-1, 1}], {n, 0, 127}];
plot = Periodogram[data, SampleRate -> 3000000/0.01,
GridLinesStyle -> Directive[Red, Dashed]];
Next, we use Position to locate the desired quantities within the plot and extract them.
plot[[First#Position[plot, Line] /. {0 -> 1} /. List -> Sequence]]
(* {{0., -8.99487}, {2.38095*10^6, 1.60543}, {4.7619*10^6, 1.82102}, ... *)

There is usually an associated function in instances like this. In this case PeriodogramArray outputs the data.
data = Table[
2 Sin[0.2 Pi n ] + Sin[0.5 Pi n] + RandomReal[{-1, 1}],
{n, 0, 127}];
Periodogram[data]
magdata = PeriodogramArray[data];
ListLinePlot[10 Log[10, magdata], PlotRange -> {{0, Length[magdata]/2}, All}]

Related

Axes numbers interfere with each other

I have a plot in Mathematica, and the problem is: the axes numbers of plot interfere with each other. How can I eliminate the middle numbers, For example, "5*10^12, 5*10^13, ..." and keep the main numbers "1*10^12, 1*10^13, ...". is there any other way to solve the problem?
Plot
Another option is to keep all the tick labels and rotate them:
xticks = Charting`ScaledTicks[{Log, Exp}][Log[min], Log[max]];
xticks[[All, 1]] = Exp#xticks[[All, 1]];
xticks[[All, 2]] = Rotate[#, Pi/2] & /# xticks[[All, 2]];
LogLogPlot[f[x], {x, min, max}, Frame -> True,
FrameTicks -> {Automatic, {xticks, Automatic}}, BaseStyle -> 18,
FrameLabel -> {"X", "Y"}]
Using a simple example, the ticks can be fixed like so. Referencing code from here and here.
First, the case showing overlapping labels.
f[x_] := x^2 + x^3
{min, max} = {10^-12, 10^-10};
LogLogPlot[f[x], {x, min, max}, Frame -> True,
BaseStyle -> 18, FrameLabel -> {"X", "Y"}]
Removing alternate labels.
xticks = Charting`ScaledTicks[{Log, Exp}][Log[min], Log[max]];
xticks[[All, 1]] = Exp#xticks[[All, 1]];
xticks[[All, 2]] = ReplacePart[xticks[[All, 2]],
Thread[Select[Range#Length#xticks, EvenQ] -> Spacer[{0, 0}]]];
LogLogPlot[f[x], {x, min, max}, Frame -> True,
FrameTicks -> {Automatic, {xticks, Automatic}},
BaseStyle -> 18, FrameLabel -> {"X", "Y"}]

Why I generated wrong Taylor series?

I want to prove the expression Limit[Sin[x*x] *Exp[-x*x]*x, x -> Infinity] ==0
So I do this Normal[ Series[ Sin[x*x] *Exp[-x*x]*x, {x, 0, 40}]].And the result
imply the expression will be infinity.
That is odd, and I make some change. Let t=x^2, then the expression will be Limit[Sin[t] *Exp[-t]*Sqrt[t], t -> Infinity]. I try again Normal[ Series[Sin[x] *Exp[-x]*Sqrt[x], {x, 0, 40}]]. That's the answer I need.
The right series
I can't figure out what's wrong. Why I can't generate series directly?
I wnat to say thay f, f1 and f2 should be the same, but they looks like this:
f=f1,but f2 diffuse
Normal[Series[Sin[x*x]*Exp[-x*x]*x, {x, 0, 40}]]
with t = x^2 is equivalent to
Normal[Series[Sin[t]*Exp[-t]*x, {x, 0, 40}]]
because x != Sqrt[x^2] e.g. for `x = -2
Results for negative t are not plotted.
Plot[Sin[t]*Exp[-t]*Sqrt[t], {t, -8, 8}]

Graphmath.Vector2 get nearest from a list of vectors

I'm actually using Graphmath.Vector2.near(a, b, r) to detect if 2 vectors are in range.
What if i've got a list of b and i need to fetch 5 closest vectors to a?
That module doesn't export any distance calculation function but it's trivial to recreate:
def distance({x, y}, {u, v}) do
dx = x - u
dy = y - v
:math.sqrt(dx * dx + dy * dy)
end
Now you can sort a list by its distance to the point and take the first 5 like this:
point = {1, 2}
list = [{3, 4}, {5, 6}, ...]
nearest =
list
|> Enum.sort_by(&distance(&1, point))
|> Enum.take(5)

Mathematica: Unusual piecewise plot issue

Hello and thanks in advance,
I came upon an issue trying to plot a piecewise function in Mathematica. When entered directly into the Plot function, the piecewise did NOT plot. However, when I defined the piecewise as a variable, it did plot. Why is this issue occurring and can I still join my plot? (I would like to join it by setting exclusions to none)
The following is my code: (I define a Maxwell strain function and am trying to model the plastic deformation of a polymer over multiple stress cycles)
z = 2*10^10;
h = 10^12;
MaxwellStrain[s_, t_] := s/z + (s*t)/h;
stress = {0, 10^7, -10^7, 5*10^6, 10^7, -5*10^6};
time = {0, 100, 200, 300, 400, 500};
strainList = Join[{0}, Table[MaxwellStrain[stress[[i + 1]], t - time[[i]]], {i, 1, 5}]];
Plot[
Piecewise[
Table[{
Total[strainList[[1 ;; i + 1]]], time[[i]] < t < time[[i + 1]]},
{i, 1, 5}
],
Exclusions -> none]
,
{t, 0, 500}
]
x = Piecewise[
Table[{
Total[strainList[[1 ;; i + 1]]], time[[i]] < t < time[[i + 1]]},
{i, 1, 5}
],
Exclusions -> none]
Plot[x, {t, 0, 500}]
The following is my output: (first plot doesn't show, the second does)
output:
Thank you for the help,
SV

Mathematica How do I plot a vector field with 1 variable?

I cannot figure out how to plot a vector field with only 1 variable. Maybe Mathematica doesn't support this. For example:
r(t) = cost j + sint i
same as
<cost, sint>
This doesn't work:
VectorPlot[{cos t, sin t}, {t, 0, 2 Pi}]
As a bonus how to take the derivative of a vector?
An easy workaround would be to use a 2D-VectorPlot with a dummy variable like this:
VectorPlot[
{Cos[t], Sin[t]}, {t, 0, 2 \[Pi]}, {s, -1/2, 1/2},
AspectRatio -> Automatic,
VectorPoints -> {15, 3},
FrameLabel -> {"t", None}
]
Or what probably makes more sense is to discretize the curve that you get when you follow the vector while increasing t. This is e.g. useful for Feynman-style Action-integrals in quantum mechanics.
Module[
{t, dt = 0.1, vectors, startpoints, startpoint, vector, spv, spvs},
vectors = Table[dt {Cos[t], Sin[t]}, {t, 0, 2 \[Pi], dt}];
startpoints = Accumulate[vectors];
spvs = Transpose[{startpoints, vectors}];
Graphics[Table[Arrow[{spv[[1]], spv[[1]] + spv[[2]]}], {spv, spvs}]]
]

Resources