0

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.

enter image description here

But when I created a wave of 100 oscillations with 2000 samples I got a frequency of 200 instead of 20.

enter image description here

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();
    }
}
codeDom
  • 1,623
  • 18
  • 54
  • 1
    The index in the frequency domain is not the spatial frequency as you interpret it. I suggest you Google for an explanation of the x axis in the DFT or FFT. – Cris Luengo May 29 '23 at 20:34
  • @CrisLuengo Google brought me [here](https://stackoverflow.com/a/4371627/7206675), but still I don't know how to change my code to get the expected result. – codeDom May 30 '23 at 07:02
  • You need to change the labels on your x axis. They are in units of 'bin', not frequency (Hz). – dmedine Jun 05 '23 at 07:16

0 Answers0