Skip to main content
explain how to capture both frequencies
Source Link
Leo Fang
  • 754
  • 8
  • 18

This is just a long supplement comment to @James Cunnane's answer which is correct. Try

dt = 1/100;
T=Pi;
ls = Table[0.1 Cos[30 x] + 2 Sin[x]^2, {x, 0, PiT, dt}];
ListPlot[ls, Mesh -> All, MeshStyle -> Red]
ListPlot[Abs[Fourier[ls]]^2, PlotRange -> {{0, 10}, {0, 1}}, 
DataRange -> {0, 1/dt}, FrameLabel -> {"Frequency", "Intensity"}, 
Mesh -> All, MeshStyle -> Red, GridLines -> {{30/(2 \[Pi])}, None}, 
Frame -> True, Joined -> True]

output

Note the only thing changed is the duration T whose inverse (1/Pi) defines the grid size in the Fourier spectrum. As a result, the desired frequency 30/Pi is now an integer multiple of the grid size and therefore can be captured correctly.

EDIT

Because the grid size 1/T=1/Pi is not fine enough, it is less easier to observe the lower frequency 2/2Pi (not 1/Pi because it's a sine square). Try to increase T to a larger value would resolve this issue if you like:

dt = 1/100;
T = 5 Pi;
ls = Table[0.1 Cos[30 x] + 2 Sin[x]^2, {x, 0, T, dt}];
ListPlot[Abs[Fourier[ls]]^2, PlotRange -> {{0, 1}, All}, 
DataRange -> {0, 1/dt}, FrameLabel -> {"Frequency", "Intensity"}, 
Mesh -> All, MeshStyle -> Red, GridLines -> {{2/(2 \[Pi])}, None}, 
Frame -> True, Joined -> True]

output2

Last comment: The reason you see a very high peak at zero frequency is due to the constant term when you write Sin[x]^2 as (1 - Cos[2 x])/2.

This is just a long comment to @James Cunnane's answer which is correct. Try

dt = 1/100;
ls = Table[0.1 Cos[30 x] + 2 Sin[x]^2, {x, 0, Pi, dt}];
ListPlot[ls, Mesh -> All, MeshStyle -> Red]
ListPlot[Abs[Fourier[ls]]^2, PlotRange -> {{0, 10}, {0, 1}}, 
DataRange -> {0, 1/dt}, FrameLabel -> {"Frequency", "Intensity"}, 
Mesh -> All, MeshStyle -> Red, GridLines -> {{30/(2 \[Pi])}, None}, 
Frame -> True, Joined -> True]

output

Note the only thing changed is the duration whose inverse (1/Pi) defines the grid size in the Fourier spectrum. As a result, the desired frequency 30/Pi is now an integer multiple of the grid size and therefore can be captured correctly.

This is just a long supplement comment to @James Cunnane's answer which is correct. Try

dt = 1/100;
T=Pi;
ls = Table[0.1 Cos[30 x] + 2 Sin[x]^2, {x, 0, T, dt}];
ListPlot[ls, Mesh -> All, MeshStyle -> Red]
ListPlot[Abs[Fourier[ls]]^2, PlotRange -> {{0, 10}, {0, 1}}, 
DataRange -> {0, 1/dt}, FrameLabel -> {"Frequency", "Intensity"}, 
Mesh -> All, MeshStyle -> Red, GridLines -> {{30/(2 \[Pi])}, None}, 
Frame -> True, Joined -> True]

output

Note the only thing changed is the duration T whose inverse (1/Pi) defines the grid size in the Fourier spectrum. As a result, the desired frequency 30/Pi is now an integer multiple of the grid size and therefore can be captured correctly.

EDIT

Because the grid size 1/T=1/Pi is not fine enough, it is less easier to observe the lower frequency 2/2Pi (not 1/Pi because it's a sine square). Try to increase T to a larger value would resolve this issue if you like:

dt = 1/100;
T = 5 Pi;
ls = Table[0.1 Cos[30 x] + 2 Sin[x]^2, {x, 0, T, dt}];
ListPlot[Abs[Fourier[ls]]^2, PlotRange -> {{0, 1}, All}, 
DataRange -> {0, 1/dt}, FrameLabel -> {"Frequency", "Intensity"}, 
Mesh -> All, MeshStyle -> Red, GridLines -> {{2/(2 \[Pi])}, None}, 
Frame -> True, Joined -> True]

output2

Last comment: The reason you see a very high peak at zero frequency is due to the constant term when you write Sin[x]^2 as (1 - Cos[2 x])/2.

Delete misleading sentences.
Source Link
Leo Fang
  • 754
  • 8
  • 18

This is just a long comment to @James Cunnane's answer which is correct. Try

dt = 1/100;
ls = Table[0.1 Cos[30 x] + 2 Sin[x]^2, {x, 0, Pi, dt}];
ListPlot[ls, Mesh -> All, MeshStyle -> Red]
ListPlot[Abs[Fourier[ls]]^2, PlotRange -> {{0, 10}, {0, 1}}, 
DataRange -> {0, 1/dt}, FrameLabel -> {"Frequency", "Intensity"}, 
Mesh -> All, MeshStyle -> Red, GridLines -> {{30/(2 \[Pi])}, None}, 
Frame -> True, Joined -> True]

output

Note the only thing changed is the duration whose inverse (1/Pi) defines the grid size in the Fourier spectrum. As a result, the desired frequency 30/Pi is now an integer multiple of 30/Pithe grid size and therefore can be captured correctly.

This is just a long comment to @James Cunnane's answer which is correct. Try

dt = 1/100;
ls = Table[0.1 Cos[30 x] + 2 Sin[x]^2, {x, 0, Pi, dt}];
ListPlot[ls, Mesh -> All, MeshStyle -> Red]
ListPlot[Abs[Fourier[ls]]^2, PlotRange -> {{0, 10}, {0, 1}}, 
DataRange -> {0, 1/dt}, FrameLabel -> {"Frequency", "Intensity"}, 
Mesh -> All, MeshStyle -> Red, GridLines -> {{30/(2 \[Pi])}, None}, 
Frame -> True, Joined -> True]

output

Note the only thing changed is the duration whose inverse (1/Pi) is now an integer multiple of 30/Pi.

This is just a long comment to @James Cunnane's answer which is correct. Try

dt = 1/100;
ls = Table[0.1 Cos[30 x] + 2 Sin[x]^2, {x, 0, Pi, dt}];
ListPlot[ls, Mesh -> All, MeshStyle -> Red]
ListPlot[Abs[Fourier[ls]]^2, PlotRange -> {{0, 10}, {0, 1}}, 
DataRange -> {0, 1/dt}, FrameLabel -> {"Frequency", "Intensity"}, 
Mesh -> All, MeshStyle -> Red, GridLines -> {{30/(2 \[Pi])}, None}, 
Frame -> True, Joined -> True]

output

Note the only thing changed is the duration whose inverse (1/Pi) defines the grid size in the Fourier spectrum. As a result, the desired frequency 30/Pi is now an integer multiple of the grid size and therefore can be captured correctly.

Source Link
Leo Fang
  • 754
  • 8
  • 18

This is just a long comment to @James Cunnane's answer which is correct. Try

dt = 1/100;
ls = Table[0.1 Cos[30 x] + 2 Sin[x]^2, {x, 0, Pi, dt}];
ListPlot[ls, Mesh -> All, MeshStyle -> Red]
ListPlot[Abs[Fourier[ls]]^2, PlotRange -> {{0, 10}, {0, 1}}, 
DataRange -> {0, 1/dt}, FrameLabel -> {"Frequency", "Intensity"}, 
Mesh -> All, MeshStyle -> Red, GridLines -> {{30/(2 \[Pi])}, None}, 
Frame -> True, Joined -> True]

output

Note the only thing changed is the duration whose inverse (1/Pi) is now an integer multiple of 30/Pi.