116 lines
4.1 KiB
JavaScript
116 lines
4.1 KiB
JavaScript
|
|
const ExtractType = {
|
|
平均值:0,
|
|
最大值:1
|
|
}
|
|
const Options = {};
|
|
|
|
function DataExtract(options={}){
|
|
this.Key = options.Key;
|
|
this.Options = Object.assign(Options, {
|
|
TaskId:0,
|
|
Period:options.Period || 500,
|
|
ExtractType:options.ExtractType || ExtractType.最大值,
|
|
SourceRange: Object.assign({},{ min:0,max:16384 },options.SourceRange),
|
|
CurrentRange:Object.assign({},{ min:0,max:16384 },options.CurrentRange),
|
|
clientPixel:options.clientPixel || 350,
|
|
Console:false
|
|
});
|
|
}
|
|
|
|
DataExtract.prototype.SetOptions = function(options = {}){
|
|
this.QueueCache.length = 0;
|
|
if(options.hasOwnProperty("Period"))this.Options.Period = options.Period;
|
|
if(options.hasOwnProperty("ExtractType"))this.Options.ExtractType = options.ExtractType;
|
|
if(options.hasOwnProperty("SourceRange"))this.Options.SourceRange = options.SourceRange;
|
|
if(options.hasOwnProperty("CurrentRange"))this.Options.CurrentRange = options.CurrentRange;
|
|
if(options.hasOwnProperty("clientPixel"))this.Options.clientPixel = options.clientPixel;
|
|
}
|
|
// 用于展示的点数
|
|
var __DisplayPoints = new Array(1).fill(0);
|
|
DataExtract.prototype.Execute = function(data,TaskId = 0){
|
|
if(!this.Switch) return;
|
|
if(this.Options.TaskId != 0 ) return;
|
|
this.Options.TaskId = TaskId;
|
|
const {sourceMin,sourceMax,currentMin,currentMax,clientPixel,ExtractType} = this.Options;
|
|
|
|
if(_DisplayPoints.length != clientPixel){
|
|
_DisplayPoints = new Array(clientPixel);
|
|
}
|
|
_DisplayPoints.fill(NaN);
|
|
if(data){
|
|
if(currentMin < 0 && currentMax < 0) {
|
|
postMessage({ key: "OnComplate", payload:[] });
|
|
return
|
|
};
|
|
// data = new Uint16Array(data.buffer);//Array.from(,x=>x/256);
|
|
var sourceSpan = sourceMax - sourceMin;
|
|
var displaySpan = currentMax - currentMin;
|
|
// 源点偏移
|
|
var currentOffset = 0;
|
|
var currentSpan = 0;
|
|
var currentSpanMin = sourceMin;
|
|
var currentSpanMax = sourceMax;
|
|
|
|
if( currentMax < currentSpanMax){
|
|
currentSpanMax = currentMax;
|
|
}
|
|
if( currentMin > currentSpanMin){
|
|
currentSpanMin = currentMin;
|
|
}
|
|
currentSpan = currentSpanMax - currentSpanMin;
|
|
|
|
currentOffset = (currentMin - sourceMin);
|
|
|
|
var pointScale = sourceSpan / data.length ; // 多少带宽一个点
|
|
var pixelScale = clientPixel / displaySpan;
|
|
// 当前FFT点数
|
|
var currentPointCount = currentSpan / sourceSpan * data.length;
|
|
// currentSpan / pointScale + Math.abs(currentOffset) / pointScale;
|
|
|
|
// 轴起始偏移
|
|
let displayOffset = 0;
|
|
let offsetPoint = 0;
|
|
|
|
if(currentOffset < 0){
|
|
displayOffset = Math.abs(Math.ceil(currentOffset * pixelScale))
|
|
}else{
|
|
offsetPoint = Math.abs(Math.round(currentOffset / pointScale))
|
|
}
|
|
if(displayOffset > 0){
|
|
_DisplayPoints.fill(NaN,0);
|
|
}
|
|
var i = 0;
|
|
if(currentPointCount > _DisplayPoints.length){
|
|
var start = 0;
|
|
for( i = 0;i<clientPixel - displayOffset;i++){
|
|
if(TASK_ID != this.Options.TaskId) return;
|
|
let end = Math.round(offsetPoint + (i) / pixelScale / pointScale );
|
|
var partial = data.slice(start,end);
|
|
start = end;
|
|
if(partial.length > 0 ){
|
|
var val = Math.max(...partial);
|
|
_DisplayPoints[displayOffset + i] = val;
|
|
}else if(start > 0 && end > 0){
|
|
break;
|
|
}
|
|
}
|
|
|
|
}else{
|
|
// 补帧
|
|
for( i = 0;i<clientPixel - displayOffset;i++){
|
|
|
|
let dataIndex = Math.round(offsetPoint + (i) / pixelScale / pointScale );
|
|
var val = data[dataIndex];
|
|
if(val == -Infinity || isNaN(val) || val === undefined){
|
|
break;
|
|
}else{
|
|
_DisplayPoints[displayOffset + i] = val;
|
|
}
|
|
}
|
|
//补帧
|
|
}
|
|
}
|
|
|
|
}
|