我想将其插值为4格网格。每个单元格只包含其中所有点的平均值。
输出应该如下所示
因此我们将整个数据转换为2x2矩阵。该矩阵的每个单元将具有平均x坐标&其中所有点的平均y坐标值。
A1 =(3,-3); A2 =(3.5,1.5)
A3 =( - 1,-3); A4 =( - 2,1)
=====我做了什么=====
avg = [[
(
( mat[row][col][0]
+ mat[row][col+1][0]
+ mat[row+1][col][0]
+ mat[row+1][col+1][0] ) / 4.0
,
( mat[row][col][1]
+ mat[row][col+1][1]
+ mat[row+1][col][1]
+ mat[row+1][col+1][1] ) / 4.0
)
for col in range(0, len(mat[0]), 2) ]
for row in range(0, len(mat), 2)
]
答案 0 :(得分:1)
我对numpy / scipy不太好,我认为这在优雅和效率方面可以大大改善,但它有效:
-> jupyter notebook with intermediate plots
最终代码:
public int samplesSinceBeat; // Tracks the # of samples since the prev beat
public float oldAverageHeartrate; // Renamed
public int samplesPerSecond=10000; // However many samples/sec
public float midpoint=750; // The midpoint
public MovingAverage averageSamples=new MovingAverage(10); // Averaging over 10 samples
public MovingAverage beatTimeAverage=new MovingAverage(4); // Averaging over 4 beats
..
int currentHeartrate = int(inString);
// Add to your moving average buffer:
averageSamples.add(currentHeartrate);
float averageHeartrate=averageSamples.getAverage();
// Bump up the number of samples since the last beat:
samplesSinceBeat++;
if(averageHeartrate >= midpoint && oldAverageHeartrate < midpoint){
// It crossed the line - we have a beat!
// The time since the last beat is therefore:
float timeBetweenBeats=(float)samplesSinceBeat / (float)samplesPerSecond;
// Add time between beats to another moving average:
beatTimeAverage.add(timeBetweenBeats);
// Reset samples since beat:
samplesSinceBeat=0;
}
oldAverageHeartrate=averageHeartrate;
// The BPM is now this:
int realtimeBPM= (int)(60f / beatTimeAverage.getAverage() );