Beispiel #1
0
        public static void Run()
        {
            decimal?[] prices = new decimal?[10] {
                13.5M, 17.8M, 92.3M, 0.1M, 15.7M,
                19.99M, 9.08M, 6.33M, 2.1M, 14.88M
            };

            Console.WriteLine(prices.WeightedMovingAverage());

            double?[] dprices = new double?[10] {
                13.5, 17.8, 92.3, 0.1, 15.7, 19.99, 9.08, 6.33, 2.1, 14.88
            };
            Console.WriteLine(dprices.WeightedMovingAverage());

            float?[] fprices = new float?[10] {
                13.5F, 17.8F, 92.3F, 0.1F, 15.7F, 19.99F, 9.08F, 6.33F, 2.1F, 14.88F
            };
            Console.WriteLine(fprices.WeightedMovingAverage());

            int?[] iprices = new int?[10] {
                13, 17, 92, 0, 15, 19, 9, 6, 2, 14
            };
            Console.WriteLine(iprices.WeightedMovingAverage());

            long?[] lprices = new long?[10] {
                13, 17, 92, 0, 15, 19, 9, 6, 2, 14
            };
            Console.WriteLine(lprices.WeightedMovingAverage());

            Console.WriteLine("Average on short-based collections");
            short[] sprices = new short[10] {
                13, 17, 92, 0, 15, 19, 9, 6, 2, 14
            };
            Console.WriteLine(sprices.Average());
        }
        public void Average_Numbers_ReturnsCorrectValue()
        {
            short[] array = new short[] { 4, 3, 7, 14 };

            short result = array.Average();

            Assert.AreEqual((short)7, result);
        }
Beispiel #3
0
        public void GetAverageRateForUser_WhenUserRatesBookReturnsValue()
        {
            const int userId = 1;
            var       books  = new[] { "1111", "1112", "1113" };
            var       rates  = new short[] { 1, 10, 5 };

            _testHelper.AddBooksRatedByUser(userId, books, rates);

            var actual = _sut.GetAverageRateForUser(userId);

            actual.ShouldSatisfyAllConditions(
                () => actual.ShouldNotBeNull(),
                () => actual.ShouldBe(Math.Round(rates.Average(x => x), 2)));
        }
Beispiel #4
0
        public bool MakeSnaps(int snapsize, int snapcount, string path)
        {
            try
            {
                SnapSize  = snapsize;
                SnapCount = snapcount;
                Path      = path;

                using (var pcm = new Mp3FileReader(path))
                {
                    Length       = pcm.TotalTime.Seconds;
                    TotalSamples = pcm.Length;

                    int samplesDesired = snapsize;
                    int blockscount    = snapcount;

                    if ((pcm.WaveFormat.SampleRate != 44100) || (pcm.WaveFormat.BitsPerSample != 16))
                    {
                        return(false);
                    }

                    for (int i = 0; i < blockscount; i++)
                    {
                        var buffer = new byte[samplesDesired * 4];
                        var left   = new short[samplesDesired];
                        var right  = new short[samplesDesired];
                        var leftd  = new double[samplesDesired];
                        var rightd = new double[samplesDesired];

                        int seekCounter = 0;

                        while (seekCounter <= 5)
                        {
                            pcm.Seek((i + 1) * (i + 1) * (i + 1) * blockscount * samplesDesired % (TotalSamples - samplesDesired), SeekOrigin.Begin);
                            seekCounter++;

                            var bytesRead = pcm.Read(buffer, 0, 4 * samplesDesired);

                            int index = 0;
                            for (int sample = 0; sample < bytesRead / 4; sample++)
                            {
                                left[sample]  = BitConverter.ToInt16(buffer, index + 0);
                                right[sample] = BitConverter.ToInt16(buffer, index + 2);
                                index        += 4;
                            }

                            if (left.Average(t => Math.Abs(t)) != 0)
                            {
                                break;
                            }
                        }

                        leftd  = Utility.Normalize(left, left.Length);
                        rightd = Utility.Normalize(right, right.Length);

                        var ft  = new FourierTransformer(samplesDesired);
                        var xxa = new Complex[samplesDesired];

                        for (int j = 0; j < samplesDesired; j++)
                        {
                            xxa[j] = new Complex(leftd[j], rightd[j]);
                        }

                        var ftt       = ft.Transform(xxa);
                        var pow_re_im = new List <double>(ftt.Length);
                        for (int j = 0; j < ftt.Length; ++j)
                        {
                            pow_re_im.Add(ComplexMath.Abs(ftt[j]));
                        }
                        fft_snaps.Add(pow_re_im);
                    }
                    pcm.Close();
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
Beispiel #5
0
            public bool MakeSnaps(int _snapsize, int _snapcount, string _path, int smooth_size)
            {
                try
                {
                    using (Mp3FileReader pcm = new Mp3FileReader(_path))
                    {
                        int samplesDesired = _snapsize;
                        int blockscount    = _snapcount;

                        snap_size     = _snapsize;
                        snap_count    = _snapcount;
                        total_samples = pcm.Length;
                        length        = pcm.TotalTime.Seconds;
                        path          = _path;

                        if ((pcm.WaveFormat.SampleRate != 44100) || (pcm.WaveFormat.BitsPerSample != 16))
                        {
                            return(false);
                        }

                        for (int i = 0; i < blockscount; i++)
                        {
                            byte[]   buffer = new byte[samplesDesired * 4];
                            short[]  left   = new short[samplesDesired];
                            short[]  right  = new short[samplesDesired];
                            double[] leftd  = new double[samplesDesired];
                            double[] rightd = new double[samplesDesired];

                            int bytesRead = 0;

                            //for (int j = 0; j < 1; j++) ///////////
                            int seek_counter = 0;
                            seek : pcm.Seek((i + 1) * (i + 1) * (i + 1) * blockscount * samplesDesired % (total_samples - samplesDesired), SeekOrigin.Begin);
                            seek_counter++;

                            bytesRead = pcm.Read(buffer, 0, 4 * samplesDesired);

                            int index = 0;
                            for (int sample = 0; sample < bytesRead / 4; sample++)
                            {
                                left[sample]  = BitConverter.ToInt16(buffer, index); index += 2;
                                right[sample] = BitConverter.ToInt16(buffer, index); index += 2;
                            }

                            if (left.Average(t => Math.Abs(t)) == 0)
                            {
                                if (seek_counter > 5)
                                {
                                    return(false);
                                }
                                else
                                {
                                    goto seek;
                                }
                            }

                            //snap_log10_energy.Add(Math.Log10(left.Average(t => Math.Abs(t))));

                            leftd  = Normalize(left, left.Length);
                            rightd = Normalize(right, right.Length);

                            //alglib.complex[] fl;
                            //alglib.fftr1d(leftd, out fl);
                            //fl[0].x;

                            FourierTransformer ft = new FourierTransformer(samplesDesired);
                            var xxa = new Complex[leftd.Length];

                            for (int j = 0; j < leftd.Length; j++)
                            {
                                xxa[j] = new Complex(leftd[j], rightd[j]);
                            }

                            var ftt = ft.Transform(xxa);

                            List <double> pow_re_im = new List <double>();
                            //List<double> arg_re_im = new List<double>();

                            ftt.ToList().ForEach(t => pow_re_im.Add(ComplexMath.Abs(t)));
                            //ftt.ToList().ForEach(t => arg_re_im.Add(ComplexMath.Arg(t)));

                            /*if (Double.IsNaN(MC_Log10_Energy(pow_re_im)))
                             *  if (seek_counter > 5)
                             *      return false;
                             *  else
                             *      goto seek;*/

                            fft_snaps.Add(pow_re_im);
                            //fft_smoothed_snaps.Add(Smoothen(pow_re_im, smooth_size));

                            //pow_re_im = Normalize(pow_re_im);

                            /*
                             * var f_pwri = pow_re_im.Average(t => Math.Abs(t));
                             * for (int k = 0; k < pow_re_im.Count; k++)
                             *  pow_re_im[k] = (Math.Abs(pow_re_im[k]) >= f_pwri * 1.5) ? pow_re_im[k] : 0;
                             */

                            /*
                             * FourierTransformer ft2 = new FourierTransformer(samplesDesired);
                             * var xx2 = new List<Complex>();
                             * for (int j = 0; j < pow_re_im.Count; j++)
                             * {
                             *  xx2.Add(new Complex(pow_re_im[j], arg_re_im[j]));
                             * }
                             * var ftt2 = ft2.Transform(xx2);
                             * //var ftt2 = ft2.Transform(ftt);
                             *
                             * List<double> pow_re_im2 = new List<double>();
                             * ftt2.ToList().ForEach(t => pow_re_im2.Add(ComplexMath.Abs(t)));
                             * pow_re_im2 = Normalize(pow_re_im2);
                             * fft2_snaps.Add(pow_re_im2);
                             * fft2_smoothed_snaps.Add(Smoothen(pow_re_im2, smooth_size));
                             */
                        }
                        pcm.Close();
                    }
                }
                catch (Exception e)
                {
                    return(false);
                }

                return(true);
            }