当我指定我的点集时,页面消失

时间:2014-04-08 07:22:34

标签: c# silverlight windows-phone-8

我正在编程一个调谐器,我想在我的全景图中创建一个图形。 但是我有一个问题,事实上当我分配我的点集时,所有的全景页面都被删除了。 那我怎么解决呢?

我的xaml代码

<controls:PanoramaItem Header="Graphic">     
      <StackPanel>      
         <Canvas Visibility="Visible" Width="530" VerticalAlignment="Center">
            <TextBlock Text="|0" Canvas.Left="0" Canvas.Top="380"/>
            <TextBlock Text="|50" Canvas.Left="25" Canvas.Top="380"/>
            <TextBlock Text="|100" Canvas.Left="50" Canvas.Top="380"/>
            <TextBlock Text="|200" Canvas.Left="100" Canvas.Top="380"/>
            <TextBlock Text="|300" Canvas.Left="150" Canvas.Top="380"/>
            <TextBlock Text="|440" Canvas.Left="220" Canvas.Top="380"/>

            <TextBlock Text="|500" Canvas.Left="250" Canvas.Top="380"/>
            <TextBlock Text="|600" Canvas.Left="300" Canvas.Top="380"/>
            <TextBlock Text="|700" Canvas.Left="350" Canvas.Top="380"/>
               <Polygon x:Name="graph"
                    Points="0,150 400,125 400,275 300,200"
                    Stroke="Brown"
                    StrokeThickness="0.4" Canvas.Left="0" Canvas.Top="0">
                  <Polygon.Fill>
                      <SolidColorBrush Color="White" Opacity="0.9"/>
                  </Polygon.Fill>
               </Polygon>
            </Canvas>
         </StackPanel>
</controls:PanoramaItem>

我的C#代码每500毫秒调用一次

double[] spectr = FftAlgorithm.Calculate(x);// To have the spectr
myPointCollection.Add(new System.Windows.Point(0, 370));
for(int i = 0; i < spectr.Length;i+= 2)
    {
            myPointCollection.Add(new System.Windows.Point(0+i/2, 370 - spectr[i]/2)); 
            if (i >= 400*1)
            {
                myPointCollection.Add(new System.Windows.Point(0+455/4,370));
                break;
            }
    }


graph.Points = myPointCollection;

提前致谢!

编辑: 我的fft算法:

struct ComplexNumber
{
    public double Re;
    public double Im;

    public ComplexNumber(double re)
    {
        this.Re = re;
        this.Im = 0;
    }

    public ComplexNumber(double re, double im)
    {
        this.Re = re;
        this.Im = im;
    }

    public static ComplexNumber operator *(ComplexNumber n1, ComplexNumber n2)
    {
        return new ComplexNumber(n1.Re * n2.Re - n1.Im * n2.Im,
            n1.Im * n2.Re + n1.Re * n2.Im);
    }

    public static ComplexNumber operator +(ComplexNumber n1, ComplexNumber n2)
    {
        return new ComplexNumber(n1.Re + n2.Re, n1.Im + n2.Im);
    }

    public static ComplexNumber operator -(ComplexNumber n1, ComplexNumber n2)
    {
        return new ComplexNumber(n1.Re - n2.Re, n1.Im - n2.Im);
    }

    public static ComplexNumber operator -(ComplexNumber n)
    {
        return new ComplexNumber(-n.Re, -n.Im);
    }

    public static implicit operator ComplexNumber(double n)
    {
        return new ComplexNumber(n, 0);
    }

    public ComplexNumber PoweredE()
    {
        double e = Math.Exp(Re);
        return new ComplexNumber(e * Math.Cos(Im), e * Math.Sin(Im));
    }

    public double Power2()
    {
        return Re * Re - Im * Im;
    }

    public double AbsPower2()
    {
        return Re * Re + Im * Im;
    }

    public override string ToString()
    {
        return String.Format("{0}+i*{1}", Re, Im);
    }
}

public static class FftAlgorithm
{
    /// <summary>
    /// Calculates FFT using Cooley-Tukey FFT algorithm.
    /// </summary>
    /// <param name="x">input data</param>
    /// <returns>spectrogram of the data</returns>
    /// <remarks>
    /// If amount of data items not equal a power of 2, then algorithm
    /// automatically pad with 0s to the lowest amount of power of 2.
    /// </remarks>
    public static double[] Calculate(double[] x)
    {
        int length;
        int bitsInLength;
        if (IsPowerOfTwo(x.Length))
        {
            length = x.Length;
            bitsInLength = Log2(length) - 1;
        }
        else
        {
            bitsInLength = Log2(x.Length);
            length = 1 << bitsInLength;
            // the items will be pad with zeros
        }

        // bit reversal
        ComplexNumber[] data = new ComplexNumber[length];
        for (int i = 0; i < x.Length; i++)
        {
            int j = ReverseBits(i, bitsInLength);
            data[j] = new ComplexNumber(x[i]);
        }

        // Cooley-Tukey 
        for (int i = 0; i < bitsInLength; i++)
        {
            int m = 1 << i;
            int n = m * 2;
            double alpha = -(2 * Math.PI / n);

            for (int k = 0; k < m; k++)
            {
                // e^(-2*pi/N*k)
                ComplexNumber oddPartMultiplier = new ComplexNumber(0, alpha * k).PoweredE();

                for (int j = k; j < length; j += n)
                {
                    ComplexNumber evenPart = data[j];
                    ComplexNumber oddPart = oddPartMultiplier * data[j + m];
                    data[j] = evenPart + oddPart;
                    data[j + m] = evenPart - oddPart;
                }
            }
        }

        // calculate spectrogram
        double[] spectrogram = new double[length];
        for (int i = 0; i < spectrogram.Length; i++)
        {

                spectrogram[i] = data[i].AbsPower2();

        }
        return spectrogram;
    }

    /// <summary>
    /// Gets number of significat bytes.
    /// </summary>
    /// <param name="n">Number</param>
    /// <returns>Amount of minimal bits to store the number.</returns>
    private static int Log2(int n)
    {
        int i = 0;
        while (n > 0)
        {
            ++i; n >>= 1;
        }
        return i;
    }

    /// <summary>
    /// Reverses bits in the number.
    /// </summary>
    /// <param name="n">Number</param>
    /// <param name="bitsCount">Significant bits in the number.</param>
    /// <returns>Reversed binary number.</returns>
    private static int ReverseBits(int n, int bitsCount)
    {
        int reversed = 0;
        for (int i = 0; i < bitsCount; i++)
        {
            int nextBit = n & 1;
            n >>= 1;

            reversed <<= 1;
            reversed |= nextBit;
        }
        return reversed;
    }

    /// <summary>
    /// Checks if number is power of 2.
    /// </summary>
    /// <param name="n">number</param>
    /// <returns>true if n=2^k and k is positive integer</returns>
    private static bool IsPowerOfTwo(int n)
    {
        return n > 1 && (n & (n - 1)) == 0;
    }
}

勇士的例子返回了这个alogorithm(在我的点集合中由2除以):

Array
7774,84102428705
3,77541922071645
2,60171097464947
4,25540621691683
5,28851887723449
3,4148780139543
2,76973970084357
1,99548270720925
2,48089729433758
1,81148493997232
4,08591050588941
3,86634237956173
2,23154042032444
2,34943177165949
0,202241877529014
0,361856279722826
0,305770091815949
1,10819055376782
1,14078528143499
0,0885925819196023
0,0589821600576737
1,82627322162955
0,510590967254011
1,09703702271706
0,624027733620266
1,38613991581386
0,61563282026637
0,833719920552669
1,72066078720428
0,61866640580564
0,311134872285707
0,545134957482531
1,32248892387885
0,280664970077368
0,288037724854005
0,478421643868339
0,0965479866862064
2,77251684855075
4,28321499123499
0,498259034734298
9,93132231440244
2,10269510682998
0,805331883010264

0 个答案:

没有答案