Skip to content

Commit f4dfb68

Browse files
authored
Added CWT Frequencies and updated corresponding methods in other objects. Also added CWTExtensions class as well (#16)
1 parent cace560 commit f4dfb68

5 files changed

Lines changed: 110 additions & 3 deletions

File tree

FCWTAPI/CWTExtensions.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace FCWTNET
8+
{
9+
public static class CWTExtensions
10+
{
11+
public static double GetFrequencyAtIndex(this CWTObject cwt, int index)
12+
{
13+
if (!cwt.Equals(null))
14+
{
15+
return cwt.FrequencyAxis.WaveletCenterFrequencies[index];
16+
}
17+
else
18+
{
19+
throw new NullReferenceException("cwt frequency axis is null");
20+
}
21+
}
22+
}
23+
}

FCWTAPI/CWTFrequencies.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace FCWTNET
8+
{
9+
public class CWTFrequencies
10+
{
11+
public double[]? WaveletCenterFrequencies { get; private set; }
12+
public int Length { get; private set; }
13+
14+
public CWTFrequencies(double[] centerFrequencies)
15+
{
16+
WaveletCenterFrequencies = centerFrequencies;
17+
Length = centerFrequencies.Length;
18+
}
19+
public CWTFrequencies()
20+
{
21+
WaveletCenterFrequencies = null;
22+
}
23+
public void ReplaceFrequencies(double[] centerFrequencies)
24+
{
25+
WaveletCenterFrequencies = centerFrequencies;
26+
}
27+
public double[] ToMZValues(double calibrationCoefficient)
28+
{
29+
// calibration formula: m/z = A/f^2
30+
if (WaveletCenterFrequencies.Equals(null))
31+
{
32+
throw new NullReferenceException("Error: WaveletCenterFrequencies null.");
33+
}
34+
double[] mzValues = new double[WaveletCenterFrequencies.Length];
35+
for(int i = 0; i < WaveletCenterFrequencies.Length; i++)
36+
{
37+
mzValues[i] = calibrationCoefficient / (WaveletCenterFrequencies[i] * WaveletCenterFrequencies[i]);
38+
}
39+
return mzValues;
40+
}
41+
public void CalculateFrequencyFromScale()
42+
{
43+
44+
}
45+
46+
}
47+
}

FCWTAPI/CWTObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class CWTObject
2727
public int? SamplingRate { get; }
2828

2929
public double[,]? OutputCWT { get; private set; }
30-
public double[]? FrequencyAxis { get; private set; }
30+
public CWTFrequencies? FrequencyAxis { get; private set; }
3131
public double[]? TimeAxis { get; private set; }
3232

3333
public CWTObject(double[] inputData, int psoctave, int pendoctave, int pnbvoice, float c0, int nthreads, bool use_optimization_schemes, int? samplingRate = null)
@@ -213,7 +213,7 @@ public void CalculateFrequencyAxis()
213213
{
214214
freqArray[i] = C0 / Math.Pow(2, (1 + (i + 1) * deltaA));
215215
}
216-
FrequencyAxis = freqArray;
216+
FrequencyAxis = new CWTFrequencies(freqArray);
217217
}
218218
public void CalculateTimeAxis()
219219
{

TestFCWTAPI/CWTObjectTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static void TestCalculateFrequencyAxis()
107107
CWTObject cosCWT = new(cosine, 1, 6, 200, (float)(2 * Math.PI), 4, false);
108108
cosCWT.CalculateFrequencyAxis();
109109
cosCWT.PerformCWT();
110-
Assert.AreEqual(cosCWT.FrequencyAxis[4], (2 * Math.PI) / Math.Pow(2, 1.025), 0.001);
110+
Assert.AreEqual(cosCWT.GetFrequencyAtIndex(4), (2 * Math.PI) / Math.Pow(2, 1.025), 0.001);
111111
Assert.AreEqual(cosCWT.OutputCWT.GetLength(0) / 2, cosCWT.FrequencyAxis.Length);
112112
}
113113
[Test]

TestFCWTAPI/TestCWTFrequencies.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using NUnit.Framework;
2+
using System;
3+
using FCWTNET;
4+
5+
6+
7+
namespace TestFCWTAPI
8+
{
9+
public class TestCWTFrequencies
10+
{
11+
[SetUp]
12+
public static void Setup()
13+
{
14+
15+
}
16+
17+
[Test]
18+
public void TestToMzValues()
19+
{
20+
double[] testFrequencies = new double[]
21+
{
22+
0.450e6,
23+
0.475e6
24+
};
25+
double calibrationCoefficient = 7.5e12;
26+
var cwtFrequencies = new CWTFrequencies(testFrequencies);
27+
var badCwtFrequencies = new CWTFrequencies();
28+
29+
Assert.Throws<NullReferenceException>(delegate {
30+
badCwtFrequencies.ToMZValues(calibrationCoefficient);
31+
});
32+
double trueValue = calibrationCoefficient / Math.Pow(testFrequencies[0], 2);
33+
double[] mzVals = cwtFrequencies.ToMZValues(calibrationCoefficient);
34+
Assert.AreEqual(trueValue, mzVals[0]);
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)