-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathSettings.cs
More file actions
71 lines (62 loc) · 2.44 KB
/
Settings.cs
File metadata and controls
71 lines (62 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Text;
namespace Spectrogram
{
class Settings
{
public readonly double SampleRate;
// vertical information
public readonly int FftSize;
public readonly double FftLengthSec;
public readonly double FreqNyquist;
public readonly double HzPerPixel;
public readonly double PxPerHz;
public readonly int FftIndex1;
public readonly int FftIndex2;
public readonly double FreqMin;
public readonly double FreqMax;
public readonly double FreqSpan;
public readonly int Height;
public int OffsetHz;
// horizontal information
public readonly double[] Window;
public readonly int StepSize;
public readonly double StepLengthSec;
public readonly double StepOverlapFrac;
public readonly double StepOverlapSec;
public Settings(double sampleRate, int fftSize, int stepSize, double minFreq, double maxFreq, int offsetHz)
{
static bool IsPowerOfTwo(int x) => ((x & (x - 1)) == 0) && (x > 0);
if (IsPowerOfTwo(fftSize) == false)
throw new ArgumentException("FFT size must be a power of 2");
// FFT info
SampleRate = sampleRate;
FftSize = fftSize;
StepSize = stepSize;
FftLengthSec = (double)fftSize / sampleRate;
// vertical
minFreq = Math.Max(minFreq, 0);
FreqNyquist = sampleRate / 2;
HzPerPixel = sampleRate / fftSize;
PxPerHz = (double)fftSize / sampleRate;
FftIndex1 = (minFreq == 0) ? 0 : (int)(minFreq / HzPerPixel);
FftIndex2 = (maxFreq >= FreqNyquist) ? fftSize / 2 : (int)(maxFreq / HzPerPixel);
Height = FftIndex2 - FftIndex1;
FreqMin = FftIndex1 * HzPerPixel;
FreqMax = FftIndex2 * HzPerPixel;
FreqSpan = FreqMax - FreqMin;
OffsetHz = offsetHz;
// horizontal
StepLengthSec = (double)StepSize / sampleRate;
var window = new FftSharp.Windows.Hanning();
Window = window.Create(fftSize);
StepOverlapSec = FftLengthSec - StepLengthSec;
StepOverlapFrac = StepOverlapSec / FftLengthSec;
}
public int PixelY(double freq)
{
return (int)(Height - (freq - FreqMin + HzPerPixel) * PxPerHz - 1);
}
}
}