Skip to content

Commit 4223deb

Browse files
author
Gary Keeble
committed
Get curve value without expo applied
Add the facility to get the curve without the expo applied, better for the spectrum analyser (maybe)? Also added frequency labels to the analyser.
1 parent 90a42b4 commit 4223deb

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

js/expo.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
*/
88
function ExpoCurve(offset, power, inputRange, outputRange, steps) {
99
var
10-
curve, inputScale;
10+
curve, inputScale, rawInputScale;
1111

1212
function lookupStraightLine(input) {
1313
return (input + offset) * inputScale;
1414
}
1515

16+
this.lookupRaw = function(input) {
17+
return (input + offset) * rawInputScale;
18+
}
19+
1620
/**
1721
* An approximation of lookupMathPow by precomputing several expo curve points and interpolating between those
1822
* points using straight line interpolation.
@@ -58,6 +62,8 @@ function ExpoCurve(offset, power, inputRange, outputRange, steps) {
5862
return result;
5963
}
6064

65+
rawInputScale = outputRange / inputRange;
66+
6167
// If steps argument isn't supplied, use a reasonable default
6268
if (steps === undefined) {
6369
steps = 12;

js/graph_spectrum.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function dataLoad(chunks, startFrameIndex, fieldIndex, curve, buffer) {
5353
var chunk = chunks[chunkIndex];
5454
for (; frameIndex < chunk.frames.length; frameIndex++) {
5555
var fieldValue = chunk.frames[frameIndex][fieldIndex];
56-
bufferData[i++] = (curve.lookup(fieldValue));
56+
bufferData[i++] = (curve.lookupRaw(fieldValue));
5757

5858
if (i >= buffer.length)
5959
break dataCollectionLoop;
@@ -91,7 +91,7 @@ function draw() {
9191
canvasCtx.fillStyle = 'rgba(255, 255, 255, .25)'; /* white */
9292
canvasCtx.fillRect(0, 0, WIDTH, HEIGHT);
9393

94-
var barWidth = (WIDTH / PLOTTED_BUFFER_LENGTH);// * 2.5;
94+
var barWidth = (WIDTH / PLOTTED_BUFFER_LENGTH) - 1;// * 2.5;
9595
var barHeight;
9696
var x = 0;
9797

@@ -101,17 +101,43 @@ function draw() {
101101
canvasCtx.fillStyle = 'rgba(0,255,0,0.3)'; /* green */
102102
canvasCtx.fillRect(x,HEIGHT-barHeight,barWidth,barHeight);
103103

104-
x += barWidth;
104+
x += barWidth + 1;
105105
}
106-
drawAxisLabel('#' + leftPad(audioIterations, "0", 7), WIDTH - 8, HEIGHT - 10);
106+
drawGridLines(options.analyserSampleRate, LEFT, TOP, WIDTH, HEIGHT);
107+
drawAxisLabel('#' + leftPad(audioIterations, "0", 7), WIDTH - 8, HEIGHT - 10, 'right');
107108
canvasCtx.restore();
108109
}
109110

111+
function drawGridLines(sampleRate, LEFT, TOP, WIDTH, HEIGHT) {
112+
113+
var ticks = 5;
114+
var frequencyInterval = (sampleRate / ticks) / 4;
115+
var frequency = 0;
116+
117+
for(var i=0; i<=ticks; i++) {
118+
canvasCtx.beginPath();
119+
canvasCtx.lineWidth = 1;
120+
canvasCtx.strokeStyle = "rgba(255,255,255,0.25)";
121+
122+
canvasCtx.moveTo(i * (WIDTH / ticks), 0);
123+
canvasCtx.lineTo(i * (WIDTH / ticks), HEIGHT);
124+
125+
canvasCtx.stroke();
126+
drawAxisLabel((frequency)+"Hz", i * (WIDTH / ticks), HEIGHT * 1.05, 'center');
127+
frequency += frequencyInterval;
128+
}
129+
}
110130

111-
function drawAxisLabel(axisLabel, X, Y) {
131+
function drawAxisLabel(axisLabel, X, Y, align) {
112132
canvasCtx.font = drawingParams.fontSizeFrameLabel + "pt " + DEFAULT_FONT_FACE;
113133
canvasCtx.fillStyle = "rgba(255,255,255,0.9)";
114-
canvasCtx.textAlign = 'right';
134+
if(align) {
135+
canvasCtx.textAlign = align;
136+
} else
137+
{
138+
canvasCtx.textAlign = 'center';
139+
}
140+
115141

116142
canvasCtx.fillText(axisLabel, X, Y);
117143
}

js/grapher.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ function FlightLogGrapher(flightLog, graphConfig, canvas, craftCanvas, options)
5353
defaultOptions = {
5454
gapless:false,
5555
drawCraft:"3D", drawPidTable:true, drawSticks:true, drawTime:true,
56-
drawAnalyser:true, // add an analyser option
56+
drawAnalyser:true, // add an analyser option
57+
analyserSampleRate:2000/*Hz*/, // the loop time for the log
5758
eraseBackground: true // Set to false if you want the graph to draw on top of an existing canvas image
5859
},
5960

0 commit comments

Comments
 (0)