using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataCollectionSystem
{
///
/// 两个通道根据RMS选择有效
///
public class AutoRange
{
//保存的数据深度 1,2 两个通道
private List[] DataStacks = new List[2]
{
new List(),
new List()
};
//
//保存的数据RMS 1,2 两个通道
private List[] ListRMSs = new List[2]
{
new List(),
new List()
};
///
/// 判断深度
///
private int deepth = 0;
///
/// 判断阈值
//0.632 3.16
///
public double threashold = 0.01f;//阈值 10mV
//通道增益
public double[] Gains { get; private set; } = new double[2]
{
0.2f,
100f
};
///
///输出通道
///
private int ChannelOutput = 1;
///
/// 输入数据
///
///
///
public void AddData(int index, double[] voltages)
{
if (index >= DataStacks.Length)
{
throw new Exception($"index {index} out of DataStacks range");
}
//移除最前列的,防止内存爆掉
if (DataStacks[index].Count >= deepth)
{
DataStacks[index].RemoveAt(0);
ListRMSs[index].RemoveAt(0);
}
DataStacks[index].Add(voltages);
double rms = GetRMS(voltages,Gains[index]);
ListRMSs[index].Add(rms);
}
///
/// 获取有效值
///
///
///
public static double GetRMS(double[] voltages,double Gain)
{
double RMS = 0;
RMS = GetRMS(voltages);
return RMS;
}
public static double GetRMS(double[] voltages)
{
double RMS = 0;
int length = voltages.Length;
double sum = 0;
for (int i = 0; i < length; i++)
{
// voltages[i] /= Gain;
double product = voltages[i] * voltages[i];
sum += product;
}
RMS = Math.Sqrt(sum / length);
return RMS;
}
///
/// 构造切换声音深度
///
///
public AutoRange(int deepth)
{
DataStacks = new List[2];
for (int i = 0; i < 2; i++)
{
DataStacks[i] = new List(); //111
}
this.deepth = deepth;
}
//0.632 3.16
double VoltageRange = 0;
private int GetOutputChanel()
{
int outchannel = this.ChannelOutput;
if (DataStacks[0].Count >= deepth)
{
//0 通道衰减
//1 通道放大
//切换逻辑 是信号很强的时候,用衰减通道,
//信号弱的时候 用放大通道.但是要超过深度了才允许切换
//ChannelOutput
int i = 0;
bool lstIntensity = ListRMSs[0][i] > threashold; //第一个
for (i = 1; i < deepth; i++) //后面的
{
bool bIntensity = ListRMSs[0][i] > threashold;
if (lstIntensity != bIntensity)
break;
}
if (i >= deepth) //3次一致
{
outchannel = lstIntensity ? 0 : 1;//信号很强大于阈值,用衰减通道
// VoltageRange = outchannel?
//0.632 3.16
}
}
return outchannel;
}
///输出数据
public double[] GetOutPutData()
{
double[] output = null;
//根据RMS值判断用哪一个通道
ChannelOutput = GetOutputChanel();
int StackLen = DataStacks[0].Count;
output = DataStacks[ChannelOutput][StackLen - 1]; //最后一个位置
return output;
}
///
/// 产生测试数据
///
///
///
///
///
public static void GenArray(double[] array, double amp, double gain, int freq)
{
int SampleRate = array.Length;
int period = SampleRate / freq;
amp = amp * gain;
for (int i = 0; i < period; i++)
{
array[i] = amp * Math.Sin(2 * Math.PI * ((double)i / period));
}
for (int i = 1; i < freq; i++)
{
Array.Copy(array, 0, array, period * i, period);
}
/*
StringBuilder sb = new StringBuilder();
for (int i = 0; i < SampleRate;i++)
{
sb.Append(array[i].ToString());
sb.Append("\r\n");
}
string wav = sb.ToString();
File.WriteAllText("wav.csv", wav);
*/
}
}
}