Below is the code I wrote to calculate FFT for a simple wave, 10 oscillations with 200 samples, as expected FFT gave the frequency as 20.
But when I created a wave of 100 oscillations with 2000 samples I got a frequency of 200 instead of 20.
Where am I wrong?
Also, what is the explanation for the fact that in the first FFT the prominent peak has a power of 7 and the second has a power of over 20?
xaml:
<Window x:Class="Audio.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ScottPlot="clr-namespace:ScottPlot;assembly=ScottPlot.WPF"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="Window1"
Width="800"
Height="450"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ScottPlot:WpfPlot x:Name="plt1" />
<ScottPlot:WpfPlot x:Name="plt2" Grid.Row="1" />
</Grid>
</Window>
cs:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
plt1.Configuration.LockVerticalAxis = true;
plt2.Configuration.LockVerticalAxis = true;
var samples = DataGen.Cos(2000, 100);
plt1.Plot.AddSignal(samples, 1);
plt1.Refresh();
// pad
var fft = samples.Concat(new[] { 0.0, 0 }).ToArray();
Fourier.ForwardReal(fft, samples.Length);
plt2.Plot.AddSignal(fft, 1);
plt2.Refresh();
}
}