forked from swharden/Spectrogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignalGenerator.cs
More file actions
32 lines (28 loc) · 991 Bytes
/
SignalGenerator.cs
File metadata and controls
32 lines (28 loc) · 991 Bytes
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
using System;
using System.Collections.Generic;
using System.Text;
namespace Spectrogram
{
public static class SignalGenerator
{
public static float[] NoisySin(
double durationSeconds = 1,
int sampleRateHz = 8000,
double signalFrequencyHz = 2000,
double noiseLevel = .1
)
{
int pointCount = (int)(durationSeconds * sampleRateHz);
float[] values = new float[pointCount];
// create a sine wave
float oscillations = (float)(signalFrequencyHz * durationSeconds);
for (int i = 0; i < values.Length; i++)
values[i] = (float)Math.Sin(((float)i / values.Length) * 2 * Math.PI * oscillations);
// add noise
Random rand = new Random();
for (int i = 0; i < values.Length; i++)
values[i] += (float)((rand.NextDouble() - .5) * noiseLevel);
return values;
}
}
}