1
$\begingroup$

Is it possible to draw a spectral histogram from two random sets ? More specifically, I have these two random functions (fmin, fmax and Nwaves would be variables in a Manipulate box) :

fmin = 0;
fmax = 4;

Amplitude[k_] := Amplitude[k] = If[k == 0, 1, RandomReal[{0.5, 1}]]
Frequency[k_, fmin_, fmax_] := Frequency[k, fmin, fmax] = If[k == 0, 1, RandomReal[{fmin, fmax}]]

where k is an integer variable going from k = 0 up to k = Nwaves. Lets take Nwaves = 50. I would like to plot the random frequencies on an horizontal axis, and their random amplitude on the vertical axis to define a spectral plot.

How can we do this plot in Mathematica ? Please, note that I'm using Mathematica version 7.

EDIT : This is almost a solution, but it's not exactly like an "histogram" (with thin vertical bars) :

Clear["Global`*"]

fmin = 0;
fmax = 4;

Amplitude[k_] := Amplitude[k] = If[k == 0, 1, RandomReal[{0.5, 1}]]
Frequency[k_, fmin_, fmax_] := Frequency[k, fmin, fmax] = If[k == 0, 1, RandomReal[{fmin, fmax}]]

ListPlot[
    Table[{Frequency[k, fmin, fmax], Amplitude[k]}, {k, 0, 50}],
    PlotRange -> {{0, 5}, {0, 1}},
    PlotStyle -> {Blue, AbsolutePointSize[10]},
    AspectRatio -> 1,
    Frame -> True,
    ImageSize -> 500
]

spectre

$\endgroup$
  • 1
    $\begingroup$ If you only have Mathematica 7, please mention this in the question. $\endgroup$ – Szabolcs Mar 31 '16 at 13:07
1
$\begingroup$

Perhaps you would like to use Histogram3D

Manipulate[
 Histogram3D[
 Table[{Frequency[k, r, fmin, fmax], Amplitude[k, r]}, {k, 0, Nwaves - 1}]],
 {{Nwaves, 1, Style["Number of waves", 10]}, 1, 50, 1, ImageSize -> Large, 
 Appearance -> {"Labeled", "Closed"}}, {{fmin, 0, 
 Style["Min frequency", 10]}, 0, fmax, 0.001, ImageSize -> Large, 
 Appearance -> {"Labeled", "Closed"}}, {{fmax, 4, 
 Style["Max frequency", 10]}, 0, 4, 0.01, ImageSize -> Large, 
 Appearance -> {"Labeled", "Closed"}}, Delimiter, 
 Button[Style["Randomize", Bold, Red, 12], {r = RandomReal[]}, 
 Appearance -> "Palette", ImageSize -> {100, 24}], 
ControlPlacement -> Bottom, FrameMargins -> None]

enter image description here

$\endgroup$
0
$\begingroup$

While not an histogram, this Manipulate is mostly fun !

Clear["Global`*"]

r = 1;

Amplitude[k_, r_] := Amplitude[k, r] = If[k == 0, 1, RandomReal[{0.5, 1}]]
Frequency[k_, r_, fmin_, fmax_] := Frequency[k, r, fmin, fmax] = If[k == 0, 1, RandomReal[{fmin, fmax}]]

Manipulate[
    ListPlot[
        Table[{Frequency[k, r, fmin, fmax], Amplitude[k, r]}, {k, 0, Nwaves - 1}],
        PlotRange -> {{0, 5}, {0, 1}},
        PlotStyle -> {Blue, AbsolutePointSize[7]},
        AspectRatio -> 1,
        Frame -> True,
        ImageSize -> 400
    ],
    {{Nwaves, 1, Style["Number of waves", 10]},
        1, 50, 1, ImageSize -> Large,
        Appearance -> {"Labeled", "Closed"}},
    {{fmin, 0, Style["Min frequency", 10]},
        0, fmax, 0.001, ImageSize -> Large,
        Appearance -> {"Labeled", "Closed"}},
    {{fmax, 4, Style["Max frequency", 10]},
        0, 4, 0.01, ImageSize -> Large,
        Appearance -> {"Labeled", "Closed"}},
    Delimiter, 
    Button[
        Style["Randomize", Bold, Red, 12],
        {r = RandomReal[]}, Appearance -> "Palette",
        ImageSize -> {100, 24}
    ],
    ControlPlacement -> Bottom,
    FrameMargins -> None
]

spectre manipulate

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.