public void setFrequencyResponse(FrequencyResponse f)
            {
                if (f.magnitudes.Count != frequencyResponseMagnitudes)
                {
                    throw new Exception("Frequency response magnitudes not of correct length!");
                }

                if (f.phases.Count != frequencyResponsePhases)
                {
                    throw new Exception("Frequency response phases not of correct length!");
                }

                this.frequencyResponse.Add(f);
            }
Beispiel #2
0
        private AbstractFunc GetFunc(FuncType type)
        {
            AbstractFunc func;

            switch (type)
            {
            case FuncType.FrequencyResponse:
                func = new FrequencyResponse(DataInfo);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(type));
            }
            return(func);
        }
            void Download()
            {
                int size = Marshal.SizeOf(typeof(Map));

                byte[] romContents   = new byte[size];
                uint   maxReadLength = 11;

                for (uint byteOffset = 0; byteOffset < size;)
                {
                    uint   readLength = (uint)Math.Min(maxReadLength, size - byteOffset);
                    byte[] tmp;
                    hwInterface.GetControllerRegister(ScopeController.FLASH, byteOffset, readLength, out tmp);
                    Array.Copy(tmp, 0, romContents, byteOffset, readLength);
                    byteOffset += readLength;
                }
                Map m = BytesToMap(romContents);

                this.plugCount       = m.plugCount;
                this.AdcTimingValue  = m.adcTimingValue;
                this.gainCalibration = new List <GainCalibration>();
                int offset = 0;

                foreach (AnalogChannel ch in AnalogChannel.List)
                {
                    foreach (double divider in SmartScope.validDividers)
                    {
                        foreach (double multiplier in SmartScope.validMultipliers)
                        {
                            GainCalibration c = new GainCalibration()
                            {
                                channel    = ch,
                                divider    = divider,
                                multiplier = multiplier
                            };
                            double[] coeff = new double[gainCalibrationCoefficients];

                            unsafe
                            {
                                for (int i = 0; i < coeff.Length; i++)
                                {
                                    coeff[i] = (double)m.gainCalibration[offset + i];
                                }
                            }
                            c.coefficients = coeff;
                            offset        += coeff.Length;

                            this.gainCalibration.Add(c);
                        }
                    }
                }
                computeDividersMultipliers();

                this.frequencyResponse = new List <FrequencyResponse>();
                int magnitudesOffset = 0;
                int phasesOffset     = 0;

                foreach (AnalogChannel ch in AnalogChannel.List)
                {
                    foreach (double multiplier in SmartScope.validMultipliers)
                    {
                        try
                        {
                            FrequencyResponse f = new FrequencyResponse()
                            {
                                channel    = ch,
                                multiplier = multiplier,
                                phases     = new Dictionary <int, float>(),
                                magnitudes = new Dictionary <int, float>()
                            };
                            unsafe
                            {
                                for (int i = 0; i < frequencyResponsePhases; i++)
                                {
                                    f.phases.Add(m.phasesIndices[phasesOffset + i], m.phases[phasesOffset + i]);
                                }
                                phasesOffset += frequencyResponsePhases;
                                for (int i = 0; i < frequencyResponseMagnitudes; i++)
                                {
                                    f.magnitudes.Add(m.magnitudesIndices[magnitudesOffset + i], m.magnitudes[magnitudesOffset + i]);
                                }
                                magnitudesOffset += frequencyResponseMagnitudes;
                            }
                            this.frequencyResponse.Add(f);
                        }
                        catch (ArgumentException e)
                        {
                            Logger.Warn(String.Format("Failed to load frequency response from ROM for channel {0:G} and multiplier {1} [{2}]", ch, multiplier, e.Message));
                        }
                    }
                }
            }
            void Upload()
            {
                //Fill ROM map structure
                Map m = new Map();

                m.plugCount      = plugCount;
                m.adcTimingValue = AdcTimingValue;
                int offset = 0;

                //FIXME: this code can be cleaner and shorter I suppose
                foreach (AnalogChannel ch in AnalogChannel.List)
                {
                    foreach (double divider in SmartScope.validDividers)
                    {
                        foreach (double multiplier in SmartScope.validMultipliers)
                        {
                            double[] coeff = this.gainCalibration.Where(x => x.channel.Value == ch.Value && x.divider == divider && x.multiplier == multiplier).First().coefficients;
                            unsafe
                            {
                                for (int i = 0; i < coeff.Length; i++)
                                {
                                    m.gainCalibration[offset + i] = (float)coeff[i];
                                }
                            }
                            offset += coeff.Length;
                        }
                    }
                }

                int magnitudesOffset = 0;
                int phasesOffset     = 0;

                foreach (AnalogChannel ch in AnalogChannel.List)
                {
                    foreach (double multiplier in SmartScope.validMultipliers)
                    {
                        try
                        {
                            FrequencyResponse f = this.frequencyResponse.Where(x => x.multiplier == multiplier && x.channel == ch).First();
                            unsafe
                            {
                                foreach (var kvp in f.magnitudes)
                                {
                                    m.magnitudesIndices[magnitudesOffset] = (ushort)kvp.Key;
                                    m.magnitudes[magnitudesOffset]        = kvp.Value;
                                    magnitudesOffset++;
                                }
                                foreach (var kvp in f.phases)
                                {
                                    m.phasesIndices[phasesOffset] = (ushort)kvp.Key;
                                    m.phases[phasesOffset]        = kvp.Value;
                                    phasesOffset++;
                                }
                            }
                        }
                        catch (InvalidOperationException)
                        {
                            Logger.Warn(String.Format("Failed to upload frequency response to ROM for channel {0:G} and multiplier {1}", ch, multiplier));
                            continue;
                        }
                    }
                }

                byte[] b = MapToBytes(m);

                uint writeOffset = (uint)Marshal.SizeOf(m.plugCount);

                while (writeOffset < b.Length)
                {
                    uint   writeLength = Math.Min(11, (uint)(b.Length - writeOffset));
                    byte[] tmp         = new byte[writeLength];
                    Array.Copy(b, writeOffset, tmp, 0, writeLength);
                    hwInterface.SetControllerRegister(ScopeController.FLASH, writeOffset, tmp);
                    writeOffset += writeLength;
                }
            }
            public void setFrequencyResponse(FrequencyResponse f)
            {
                if (f.magnitudes.Count != frequencyResponseMagnitudes)
                    throw new Exception("Frequency response magnitudes not of correct length!");

                if (f.phases.Count != frequencyResponsePhases)
                    throw new Exception("Frequency response phases not of correct length!");

                this.frequencyResponse.Add(f);
            }
            void Download()
            {
                int size = Marshal.SizeOf(typeof(Map));
                byte[] romContents = new byte[size];
                uint maxReadLength = 11;
                for (uint byteOffset = 0; byteOffset < size; )
                {
                    uint readLength = (uint)Math.Min(maxReadLength, size - byteOffset);
                    byte[] tmp;
                    hwInterface.GetControllerRegister(ScopeController.FLASH, byteOffset, readLength, out tmp);
                    Array.Copy(tmp, 0, romContents, byteOffset, readLength);
                    byteOffset += readLength;
                }
                Map m = BytesToMap(romContents);
                this.plugCount = m.plugCount;
                this.AdcTimingValue = m.adcTimingValue;
                this.gainCalibration = new List<GainCalibration>();
                int offset = 0;
                foreach (AnalogChannel ch in AnalogChannel.List)
                {
                    foreach (double divider in SmartScope.validDividers)
                    {
                        foreach (double multiplier in SmartScope.validMultipliers)
                        {
                            GainCalibration c = new GainCalibration()
                            {
                                channel = ch,
                                divider = divider,
                                multiplier = multiplier
                            };
                            double[] coeff = new double[gainCalibrationCoefficients];

                            unsafe
                            {
                                for (int i = 0; i < coeff.Length; i++)
                                    coeff[i] = (double)m.gainCalibration[offset + i];
                            }
                            c.coefficients = coeff;
                            offset += coeff.Length;

                            this.gainCalibration.Add(c);
                        }
                    }
                }
                computeDividersMultipliers();

                this.frequencyResponse = new List<FrequencyResponse>();
                int magnitudesOffset = 0;
                int phasesOffset = 0;
                foreach (AnalogChannel ch in AnalogChannel.List)
                {
                    foreach (double multiplier in SmartScope.validMultipliers)
                    {
                        try
                        {
                            FrequencyResponse f = new FrequencyResponse()
                            {
                                channel = ch,
                                multiplier = multiplier,
                                phases = new Dictionary<int, float>(),
                                magnitudes = new Dictionary<int, float>()
                            };
                            unsafe
                            {
                                for (int i = 0; i < frequencyResponsePhases; i++)
                                    f.phases.Add(m.phasesIndices[phasesOffset + i], m.phases[phasesOffset + i]);
                                phasesOffset += frequencyResponsePhases;
                                for (int i = 0; i < frequencyResponseMagnitudes; i++)
                                    f.magnitudes.Add(m.magnitudesIndices[magnitudesOffset + i], m.magnitudes[magnitudesOffset + i]);
                                magnitudesOffset += frequencyResponseMagnitudes;
                            }
                            this.frequencyResponse.Add(f);
                        }
                        catch (ArgumentException e)
                        {
                            Logger.Warn(String.Format("Failed to load frequency response from ROM for channel {0:G} and multiplier {1} [{2}]", ch, multiplier, e.Message));
                        }
                    }
                }
            }