向波形数据添加间隙以创建间隙-JavaScript问题

时间:2018-11-05 15:56:35

标签: javascript while-loop

我正在基于示例数据文件使用javascript创建波形。 为了在样本数据之间添加间隙,我想将一些值推为零。

我的问题是,此计算不适用于像素比率大于1的情况。

因此有时计算结果不是:1 1 0 1 1 0 1 1 应该是:1 1 0 1 1 1 0 1 0

此ic由以下计算引起:

intCanvasWidth = 1024;
intPixelRatio = 1.5;
intWidth = Math.round(intCanvasWidth * intPixelRatio);
zoom = this.width/intWidth;

a = 2;
b = 1;
c = true;

sampleData = [231.79723281227552, 231.79723281227552, 230.6445414497504, 228.3391587247001, 225.92636220255343, 223.496707014168, 221.63626235791907, 219.32708450387713, 219.41920865348084, 219.91672436151782, 222.55929360596537, 225.65428902064787, 227.43910673489125, 228.03972299018733, 229.7677732493945, 232.77162541681457, 234.99187327678297, 235.34915234834094, 236.39590149169015, 237.18149376136074, 237.2532170087046, 236.75229595760928, 234.4145359484644, 229.46022870186854, 224.7549653560256, 223.2226490324722, 223.7567669995262, 226.50579137979952, 229.92924639246746, 233.94913748983507, 238.36823866062664, 231.80983201113833, 235.1453856293097, 235.32455406984914, 238.12998324176098, 237.94559621740507, 237.50473210319456, 236.31014622659995, 235.54694642491694, 234.95394978468119, 234.35384967952862, 233.475948575623, 233.09044660742467, 234.36080023446215, 237.28444284498272, 235.895451360392, 234.43544438754472, 232.32208973900248, 240.2294106237668, 237.71212988923406, 235.53094559083303, 234.3441515215442, 235.12810446758297, 236.36137990251427, 237.33425123420906, 239.593810831278, 231.24294242833543, 234.6060405304731, 235.67163281728574, 233.1012742132168, 235.19722421264956, 234.94281424956665, 233.3037826393105, 233.49350651820072, 234.6717373858941, 235.1657591152049, 234.1360620136357, 234.70976940156746, 232.9691603975347, 232.42065613572186, 233.64102817693004, 237.68930045414672, 238.0841573693969, 237.91245154586602, 237.08099920321638, 235.4133850793004, 232.21503131457177, 234.79175838243265, 236.1056507958893, 237.1098459862211, 237.23891599312194, 236.98771562520454, 235.99239999155594, 235.121800927903, 233.94105174247608, 234.55854139665385, 232.90231918180612, 232.97840084754273, 232.9880432303011, 233.36027697271754, 234.04089846698358, 234.64351019959884, 235.8866898313285, 236.1935932402503, 235.2061408855434, 234.0808007272707, 232.7069709518782, 233.21556665391176, 235.0445994901862, 238.51345505137823];

if(a>0) {
    var i = sampleData.length;
    var peaks = [];
    while(i--) {
        if(b>0) {

            if(i%(a+b)<b) {
                if(peaks.length&&c) {
                var sum=avg=0,k=peaks.length; 
                for(j in peaks)sum+=peaks[j];
                avg=Math.round(sum/k)||0;
                while(k--)sampleData[i+k+1]=avg;
                peaks = [];
                }
                sampleData[i] = 0;
            } else {
                if(c)peaks.push(sampleData[i]);
            }
        } else if(c) {
            peaks.push(sampleData[i]);
            if(!Math.round(i%a)&&peaks.length) {
            var sum=avg=0,k=peaks.length; 
            for(j in peaks)sum+=peaks[j];
            avg=Math.round(sum/k)||0;
            while(k--)sampleData[i+k+1]=avg;
            peaks = [];
            }
        }
    }
}

任何解决这个数学问题的想法都会受到赞赏。

谢谢。

0 个答案:

没有答案