/// <summary> /// Compute the dct of a given vector /// </summary> /// <param name="featureVector">vector of input series</param> /// <returns>the dct of R</returns> internal static Digest ComputeDct(float[] featureVector) { int N = featureVector.Length; float[] R = featureVector; float[] coefficients = new float[Digest.LENGTH]; float max = 0f; float min = 0f; float div2n = MathF.PI / (2 * N); float divSq = 1 / MathF.Sqrt(N); for (int k = 0; k < Digest.LENGTH; k++) { float sum = 0f; for (int n = 0; n < N; n++) { sum += R[n] * MathF.Cos((2 * n + 1) * k * div2n); } float v = coefficients[k] = sum * divSq * (k == 0 ? 1 : SQRT_TWO); max = Math.Max(max, v); min = Math.Min(min, v); } return(NormalizeDigest(coefficients, max, min)); }
/// <summary> /// return dct matrix, C Return DCT matrix of square size, <paramref name="size" /> /// </summary> /// <param name="size">int denoting the size of the square matrix to create.</param> /// <returns>size <paramref name="size" />x<paramref name="size" /> containing the dct matrix</returns> internal static FloatImage CreateDctMatrix(int size) { FloatImage ret = new FloatImage(size, size, 1 / (float)Math.Sqrt(size)); float c1 = MathF.Sqrt(2f / size); float rad = MathF.PI / 2 / size; for (int x = 0; x < size; x++) { for (int y = 1; y < size; y++) { ret[x, y] = c1 * MathF.Cos(rad * y * (2 * x + 1)); } } return(ret); }