This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
/
SquareGenerator.cs
106 lines (90 loc) · 3.1 KB
/
SquareGenerator.cs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Accord Audio Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2015
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
namespace Accord.Audio.Generators
{
using System;
/// <summary>
/// Square Signal Generator
/// </summary>
public class SquareGenerator : ISignalGenerator
{
/// <summary>
/// Gets or sets the Frequency of the squared signal.
/// </summary>
///
public double Frequency { get; set; }
/// <summary>
/// Gets or sets the Amplitude of the squared signal.
/// </summary>
///
public double Amplitude { get; set; }
/// <summary>
/// Gets or sets the Sampling Rate of the generated signals.
/// </summary>
///
public int SamplingRate { get; set; }
/// <summary>
/// Gets or sets the number of channels for the generated signals.
/// </summary>
///
public int Channels { get; set; }
/// <summary>
/// Gets or sets the sample format for created signals.
/// </summary>
///
public SampleFormat Format { get; set; }
/// <summary>
/// Creates a new Square Signal Generator.
/// </summary>
///
public SquareGenerator()
{
this.Format = SampleFormat.Format32BitIeeeFloat;
this.Channels = 1;
}
/// <summary>
/// Generates a signal.
/// </summary>
///
public Signal Generate(int samples)
{
Signal signal = new Signal(Channels, samples, SamplingRate, Format);
if (Format == SampleFormat.Format32BitIeeeFloat)
{
unsafe
{
float* dst = (float*)signal.Data.ToPointer();
float p = (float)(Frequency / SamplingRate);
float a = 2f * (float)Amplitude;
for (int i = 0; i < signal.Length; i++)
{
float q = i * p;
float t = a * (q - (float)Math.Round(q));
for (int c = 0; c < signal.Channels; c++, dst++)
*dst = t;
}
}
}
return signal;
}
}
}