Given the following set of xs and ys:
xs = [8294400, 2073600, 921600, 409920]
ys = [124, 433, 853, 1449]
Fitting this with a power law in Excel yields a good approximation:
Excel found a function of the form a(x^b). How can one determine a and b in C#? I tried using Math.Net numerics but I don't see any method that would work for a function of this form. All the functions in the Linear Regression module just find linear coefficients to functions of various forms but none seem to be able to determine an exponent.
The equation you want looks like this:
y = a*x^b
Take the natural log of both sides:
ln(y) = ln(a*x^b) = ln(a) + b*ln(x)
Now you can use linear regression on the new transformed variables (ln(x), ln(y)) and calculate the two parameters you want: ln(a) and b.
In an exponential system, the best way to do a regression is probably by doing a linear regression on a log scale. To clarify, even though your function isn't linear, taking the natural log of both sides of the equation will result in a more linear system
Non-linear function: y = a x^b
This then becomes ln(y) = ln(a x^b) = ln(a) b ln(x)
In Math.NET Numerics, a good way to code it could be:
var y = y.Select(r => Math.Log(r)).ToArray(); // transform y = ln(z)
double[] w = Fit.LinearCombination(xy, z_hat,
d => 1.0,
d => Math.Log(d[0]),
d => Math.Log(d[1]))
Or if you wanted to have it return a function you could use LinearCombinationFunc()
Sources: http://numerics.mathdotnet.com/Regression.html and http://numerics.mathdotnet.com/api/MathNet.Numerics/Fit.htm
Related
I have been testing the sample Kernel Support Vector Machines for regression problems and I would like to know how do you get the equation of the model.
For example, if the machine is created using a polynomial kernel (degree = 1), how do you get the line equation (mx + b) of this model. Is there any method in the SupportVectorMachine Class to get the model equation? or is there any way to calculate the parameters of the equation from the variables obtained after the machine is created.
Thanks in advance.
Looks like you can use this method below:
ToWeights(), which
Converts a Linear-kernel machine into an array of linear coefficients.
The first position in the array is the Threshold value.
So in your language, the first position in the array is the bias b and the rest are your linear coefficients m.
I got weird coefficients from ToWeights() when using SequentialMinimalOptimization() from which I couldn't derive the hyperplane equation. Using LinearCoordinateDescent() yielded usable coefficients for the model, however, in the form of [a,b,c...] which could be plugged in as 0 = a + bx + cy + ...
Hope that helps!
As #zrolfs noted, if you are using Accord.NET with Sequential Minimal Optimization, the ToWeights() function does not currently return relevant coefficients for the decision function. Nevertheless, you can calculate these coefficients directly. In order to do so, multiply the SVM weights vector by the matrix of support vectors, like so:
double[] DecisionFunctionCoefficients = new double[dwTotalFeatures];
for (int iFeature = 0; iFeature < dwTotalFeatures; iFeature++) {
for (int iVector = 0; iVector < SVM.SupportVectors.Length; iVector++) {
DecisionFunctionCoefficients[iFeature] += (SVM.SupportVectors[iVector][iFeature] * SVM.Weights[iVector]);
}
}
I'm trying to get the pitch from the microphone input. First I have decomposed the signal from time domain to frequency domain through FFT. I have applied Hamming window to the signal before performing FFT. Then I get the complex results of FFT. Then I passed the results to Harmonic product spectrum, where the results get downsampled and then multiplied the downsampled peaks and gave a value as a complex number. Then what should I do to get the fundamental frequency?
public float[] HarmonicProductSpectrum(Complex[] data)
{
Complex[] hps2 = Downsample(data, 2);
Complex[] hps3 = Downsample(data, 3);
Complex[] hps4 = Downsample(data, 4);
Complex[] hps5 = Downsample(data, 5);
float[] array = new float[hps5.Length];
for (int i = 0; i < array.Length; i++)
{
checked
{
array[i] = data[i].X * hps2[i].X * hps3[i].X * hps4[i].X * hps5[i].X;
}
}
return array;
}
public Complex[] Downsample(Complex[] data, int n)
{
Complex[] array = new Complex[Convert.ToInt32(Math.Ceiling(data.Length * 1.0 / n))];
for (int i = 0; i < array.Length; i++)
{
array[i].X = data[i * n].X;
}
return array;
}
I have tried to get the magnitude using,
magnitude[i] = (float)Math.Sqrt(array[i] * array[i] + (data[i].Y * data[i].Y));
inside the for loop in HarmonicProductSpectrum method. Then tried to get the maximum bin using,
float max_mag = float.MinValue;
float max_index = -1;
for (int i = 0; i < array.Length / 2; i++)
if (magnitude[i] > max_mag)
{
max_mag = magnitude[i];
max_index = i;
}
and then I tried to get the frequency using,
var frequency = max_index * 44100 / 1024;
But I was getting garbage values like 1248.926, 1205,859, 2454.785 for the A4 note (440 Hz) and those values don't look like harmonics of A4.
A help would be greatly appreciated.
I implemented harmonic product spectrum in Python to make sure your data and algorithm were working nicely.
Here’s what I see when applying harmonic product spectrum to the full dataset, Hamming-windowed, with 5 downsample–multiply stages:
This is just the bottom kilohertz, but the spectrum is pretty much dead above 1 KHz.
If I chunk up the long audio clip into 8192-sample chunks (with 4096-sample 50% overlap) and Hamming-window each chunk and run HPS on it, this is the matrix of HPS. This is kind of a movie of the HPS spectrum over the entire dataset. The fundamental frequency seems to be quite stable.
The full source code is here—there’s a lot of code that helps chunk the data and visualize the output of HPS running on the chunks, but the core HPS function, starting at def hps(…, is short. But it has a couple of tricks in it.
Given the strange frequencies that you’re finding the peak at, it could be that you’re operating on the full spectrum, from 0 to 44.1 KHz? You want to only keep the “positive” frequencies, i.e., from 0 to 22.05 KHz, and apply the HPS algorithm (downsample–multiply) on that.
But assuming you start out with a positive-frequency-only spectrum, take its magnitude properly, it looks like you should get reasonable results. Try to save out the output of your HarmonicProductSpectrum to see if it’s anything like the above.
Again, the full source code is at https://gist.github.com/fasiha/957035272009eb1c9eb370936a6af2eb. (There I try out another couple of spectral estimator, Welch’s method from Scipy and my port of the Blackman-Tukey spectral estimator. I’m not sure if you are set on implementing HPS or if you would consider other pitch estimators, so I’m leaving the Welch/Blackman-Tukey results there.)
Original I wrote this as a comment but had to keep revising it because it was confusing so here’s it as a mini-answer.
Based on my brief reading of this intro to HPS, I don’t think you’re taking the magnitudes correctly after you find the four decimated responses.
You want:
array[i] = sqrt(data[i] * Complex.conjugate(data[i]) *
hps2[i] * Complex.conjugate(hps2[i]) *
hps3[i] * Complex.conjugate(hps3[i]) *
hps4[i] * Complex.conjugate(hps4[i]) *
hps5[i] * Complex.conjugate(hps5[i])).X;
This uses the sqrt(x * Complex.conjugate(x)) trick to find x’s magnitude, and then multiplies all 5 magnitudes.
(Actually, it moves the sqrt outside the product, so you only do one sqrt, saves some time, but gives the same result. So maybe that’s another trick.)
Final trick: it takes that result’s real part because sometimes due to float accuracy issues, a tiny imaginary component, like 1e-15, survives.
After you do this, array should contain just real floats, and you can apply the max-bin-finding.
If there’s no Conjugate method, then the old-fashioned way should work:
public float mag2(Complex c) { return c.X * c.X + c.Y * c.Y; }
// in HarmonicProductSpectrum
array[i] = sqrt(mag2(data[i]) * mag2(hps2[i]) * mag2(hps3[i]) * mag2(hps4[i]) * mag2(hps5[i]));
There’s algebraic flaws with the two approaches you suggested in the comments below, but the above should be correct. I’m not sure what C# does when you assign a Complex to a float—maybe it uses the real component? I’d have thought that’d be a compiler error, but with the above code, you’re doing the right thing with the complex data, and only assigning a float to array[i].
To get a pitch estimate, you have to divide your sumed bin frequency estimate by the downsampling ratio used for that sum.
Added: You should also sum the magnitudes (abs()), not take the magnitude of the complex sum.
But the harmonic product spectrum algorithm (HPS), especially when using only integer ratios of downsampling, doesn't usually provide better pitch estimation resolution. Instead, it provides a more robust rough pitch estimate (less likely to be fooled by a harmonic) than using a single bare FFT magnitude peak for sequential overtone rich timbres that have weak or missing fundamental spectral content.
If you know how to downsample a spectrum by fractional ratios (using interpolation, etc.), you can try finer grained downsampling to get a better pitch estimate out of HPS. Or you can use an HPS result to inform you of a narrower frequency range in which to search using another pitch or frequency estimation method.
for (double x=0;x<=7D;x+=.01D)
{
b = 1.771289; c = 2.335719; d = 0.5855771; g = 4.4990302; h = 4.3369349; k = 0.67356705;
y = b * Math.Exp(-(0.5 * (Math.Pow(((x - c) / d), 2)))) +
g * Math.Exp(-(0.5 * (Math.Pow(((x - h) / k), 2))));
qResults.Rows.Add(x, y);
}
the graph is good but it draws a hole in the peek.i am using mschart:
http://imageshack.us/photo/my-images/824/graph1v.png/
i would like to know whether the hole is a problem with my syntax?
It seems that your y-axis range is bounded by the maximum value, but the very point falls exactly outside the plotting range.
One solution is to add a small amount to the axis range such that all points fall clearly inside the plotting space.
Try making the max y range for the graph a little over the max value. If the max value is 4.5 then make the graph y-axis limit equal to 5.0.
There's nothing wrong with your syntax, that really should be a smooth curve. I stuck it into matlab just to be certain.
I have a set P of 2D points that I could like to cluster in a 2D uniformly spaced grid, where each cell is length X.
I want to do this because I am trying to create a heat map, and I have way to much information so I am hoping by clustering the points into a uniformly spaced grid I can just report the final count of each grid.
Thanks!
if It makes any difference I am getting my information via SQL (the points) that are within a certain radius of a specified point first prior to subdivision.
Are you looking for something like this?
var result = from p in points
group p by new { X = p.X / length, Y = p.Y / length } into g
select new
{
g.Key.X,
g.Key.Y,
Count = g.Count()
};
I don't know if there's a way to take advantage of the order of points.
I've got this code snippet, and I'm wondering why the results of the first method differ from the results of the second method, given the same input?
public double AngleBetween_1(vector a, vector b) {
var dotProd = a.Dot(b);
var lenProd = a.Len*b.Len;
var divOperation = dotProd/lenProd;
return Math.Acos(divOperation) * (180.0 / Math.PI);
}
public double AngleBetween_2(vector a, vector b) {
var dotProd = a.Dot(b);
var lenProd = a.Len*b.Len;
var divOperation = dotProd/lenProd;
return (1/Math.Cos(divOperation)) * (180.0 / Math.PI);
}
It's because the first method is correct, while the second method is incorrect.
You may notice that the arccosine function is sometimes written "acos" and sometimes written "cos-1". This is a quirk of mathematical notation: "cos-1" is really the arccosine and NOT the reciprocal of the cosine (which is the secant).
However, if you ever see "cos2", then that's the square of the cosine, and "cos3" is the cube of the cosine. The notation for trigonometric functions is weird this way. Most operators use superscripts to indicate repeated application.
Math.Acos(divOperation) isn't equivalent to 1/Math.Cos(divOperation). arccos is the inverse function of cos, not the multiplicative inverse.
Probably because acos(x) ≠ 1/cos(x).