-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WaveformGraph.linq
71 lines (60 loc) · 1.88 KB
/
WaveformGraph.linq
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
<Query Kind="Program" />
void Main()
{
var frequency = 500;
Enumerable.Range(0, frequency).Chart(x => x)
.AddYSeries(x => GetSine(x, frequency), LINQPad.Util.SeriesType.Line, "Sine")
.AddYSeries(x => GetSquare(x, frequency), LINQPad.Util.SeriesType.Line, "Square")
.AddYSeries(x => GetSquarePulseWidth(x, frequency, 0.75f), LINQPad.Util.SeriesType.Line, "Square PW")
.AddYSeries(x => GetTriangle(x, frequency), LINQPad.Util.SeriesType.Line, "Triangle")
.AddYSeries(x => GetSawToth(x, frequency), LINQPad.Util.SeriesType.Line, "Saw")
.Dump();
}
private const double TwoPi = 2 * Math.PI;
private const int SampleRate = 44100;
private const double Gain = 1;
double GetSquarePulseWidth(int nSample, double frequency, float pulseWidth)
{
var multiple = 2 * frequency / SampleRate;
var sampleSaw = nSample * multiple % 2;
var sampleValue = sampleSaw >= pulseWidth * 2 ? -Gain : Gain;
nSample++;
sampleSaw.Dump();
return sampleValue;
}
double GetSine(int nSample, double frequency)
{
var multiple = TwoPi * frequency / SampleRate;
var sampleValue = Gain * Math.Sin(nSample * multiple);
nSample++;
return sampleValue;
}
double GetSquare(int nSample, double frequency)
{
var multiple = 2 * frequency / SampleRate;
var sampleSaw = nSample * multiple % 2 - 1;
var sampleValue = sampleSaw >= 0 ? Gain : -Gain;
nSample++;
return sampleValue;
}
double GetTriangle(int nSample, double frequency)
{
var multiple = 2 * frequency / SampleRate;
var sampleSaw = nSample * multiple % 2;
var sampleValue = 2 * sampleSaw;
if (sampleValue > 1)
sampleValue = 2 - sampleValue;
if (sampleValue < -1)
sampleValue = -2 - sampleValue;
sampleValue *= Gain;
nSample++;
return sampleValue;
}
double GetSawToth(int nSample, double frequency)
{
var multiple = 2 * frequency / SampleRate;
var sampleSaw = nSample * multiple % 2 - 1;
var sampleValue = Gain * sampleSaw;
nSample++;
return sampleValue;
}