Some minor bug fixes and increase dx
This commit is contained in:
parent
bc02eec44d
commit
75a2b21764
@ -27,6 +27,8 @@
|
|||||||
margin-right: auto;
|
margin-right: auto;
|
||||||
max-width: 1300px;
|
max-width: 1300px;
|
||||||
min-height: 100%;
|
min-height: 100%;
|
||||||
|
padding-left: 2rem;
|
||||||
|
padding-right: 2rem;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
@ -2,11 +2,10 @@ const RENDER_TYPE = {
|
|||||||
LATEX: 1,
|
LATEX: 1,
|
||||||
FUNC: 2,
|
FUNC: 2,
|
||||||
};
|
};
|
||||||
const THRESHOLD = 1e-10;
|
const THRESHOLD = 1e-12;
|
||||||
const LATEX_SIGFIGS = 8;
|
|
||||||
const FONT = "Courier New";
|
const FONT = "Courier New";
|
||||||
const FONT_HEIGHT_PX = 16;
|
const FONT_HEIGHT_PX = 24;
|
||||||
const DX = 3;
|
const DX = 4;
|
||||||
|
|
||||||
const canvas = document.getElementById("canvas");
|
const canvas = document.getElementById("canvas");
|
||||||
const ctx = canvas.getContext("2d");
|
const ctx = canvas.getContext("2d");
|
||||||
@ -33,17 +32,12 @@ const initializeState = () => {
|
|||||||
height: canvas.parentElement.clientHeight,
|
height: canvas.parentElement.clientHeight,
|
||||||
xLabels,
|
xLabels,
|
||||||
yLabels,
|
yLabels,
|
||||||
gridBox: { width: 0.9, height: 0.9 },
|
yLabelPadding: 12,
|
||||||
heights: Array(xLabels.length * DX - 1).fill(0),
|
heights: Array(xLabels.length * DX - 1).fill(0),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const dft = (
|
const dft = (heights, render = RENDER_TYPE.LATEX, threshold = THRESHOLD) => {
|
||||||
heights,
|
|
||||||
render = RENDER_TYPE.LATEX,
|
|
||||||
threshold = THRESHOLD,
|
|
||||||
sigfigs = LATEX_SIGFIGS
|
|
||||||
) => {
|
|
||||||
const n = heights.length;
|
const n = heights.length;
|
||||||
return Array(n)
|
return Array(n)
|
||||||
.fill()
|
.fill()
|
||||||
@ -67,9 +61,9 @@ const dft = (
|
|||||||
|
|
||||||
switch (render) {
|
switch (render) {
|
||||||
case RENDER_TYPE.LATEX:
|
case RENDER_TYPE.LATEX:
|
||||||
return `${amp.toPrecision(sigfigs)}\\cos\\left(${w}x\\frac{2}{${
|
return `${amp}\\cos\\left(${w}\\frac{${
|
||||||
n / DX
|
2 * DX
|
||||||
}}\\pi+${phase.toPrecision(sigfigs)}\\right)`;
|
}\\pi}{${n}}x+${phase}\\right)`;
|
||||||
case RENDER_TYPE.FUNC:
|
case RENDER_TYPE.FUNC:
|
||||||
return (t) => amp * Math.cos(w * t + phase);
|
return (t) => amp * Math.cos(w * t + phase);
|
||||||
}
|
}
|
||||||
@ -91,21 +85,23 @@ const loop = () => {
|
|||||||
state.diff.width ||
|
state.diff.width ||
|
||||||
state.diff.height ||
|
state.diff.height ||
|
||||||
state.diff.xLabels ||
|
state.diff.xLabels ||
|
||||||
state.diff.yLabels ||
|
state.diff.yLabels
|
||||||
state.diff.gridBox
|
|
||||||
) {
|
) {
|
||||||
resizeCanvas(state.diff);
|
resizeCanvas(state.diff);
|
||||||
state.gridBoxWidth = state.gridBox.width * state.width;
|
ctx.font = `${FONT_HEIGHT_PX}px ${FONT}`;
|
||||||
state.gridBoxHeight = state.gridBox.height * state.height;
|
|
||||||
|
|
||||||
state.maxYLabelWidth = state.yLabels.reduce(
|
state.maxYLabelWidth = state.yLabels.reduce(
|
||||||
(a, label) => Math.max(ctx.measureText(label).width, a),
|
(a, label) => Math.max(ctx.measureText(label).width, a),
|
||||||
-Infinity
|
-Infinity
|
||||||
);
|
);
|
||||||
|
console.log(state.maxYLabelWidth);
|
||||||
|
|
||||||
|
state.gridBoxWidth =
|
||||||
|
state.width - state.maxYLabelWidth - state.yLabelPadding;
|
||||||
|
state.gridBoxHeight = state.height - 2.5 * FONT_HEIGHT_PX; // 2.5 to include bottom part of tall letters ("g", "y", etc.)
|
||||||
|
|
||||||
state.topLeftGridPos = {
|
state.topLeftGridPos = {
|
||||||
x: (state.width - state.gridBoxWidth) / 2 + state.maxYLabelWidth,
|
x: state.maxYLabelWidth + state.yLabelPadding,
|
||||||
y: (state.height - state.gridBoxHeight) / 2,
|
y: FONT_HEIGHT_PX,
|
||||||
};
|
};
|
||||||
|
|
||||||
state.bottomRightGridPos = {
|
state.bottomRightGridPos = {
|
||||||
@ -135,12 +131,9 @@ const drawDividers = (
|
|||||||
bottomRightGridPos,
|
bottomRightGridPos,
|
||||||
yLabelPadding
|
yLabelPadding
|
||||||
) => {
|
) => {
|
||||||
|
ctx.font = `${FONT_HEIGHT_PX}px ${FONT}`;
|
||||||
xDividers.forEach(({ label, position }) => {
|
xDividers.forEach(({ label, position }) => {
|
||||||
ctx.fillText(
|
ctx.fillText(label, position.x - ctx.measureText(label).width, position.y);
|
||||||
label,
|
|
||||||
position.x - ctx.measureText(label).width / 2,
|
|
||||||
position.y
|
|
||||||
);
|
|
||||||
drawLine(
|
drawLine(
|
||||||
{ ...position, y: topLeftGridPos.y },
|
{ ...position, y: topLeftGridPos.y },
|
||||||
{ ...position, y: bottomRightGridPos.y }
|
{ ...position, y: bottomRightGridPos.y }
|
||||||
@ -173,8 +166,6 @@ const draw = ({
|
|||||||
}) => {
|
}) => {
|
||||||
ctx.clearRect(0, 0, width, height);
|
ctx.clearRect(0, 0, width, height);
|
||||||
|
|
||||||
ctx.font = `${FONT_HEIGHT_PX}px ${FONT}`;
|
|
||||||
|
|
||||||
const xDividers = xLabels.map((label, i) => ({
|
const xDividers = xLabels.map((label, i) => ({
|
||||||
label,
|
label,
|
||||||
position: {
|
position: {
|
||||||
@ -192,7 +183,8 @@ const draw = ({
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
drawDividers(xDividers, yDividers, topLeftGridPos, bottomRightGridPos, 12);
|
drawDividers(xDividers, yDividers, topLeftGridPos, bottomRightGridPos, 12);
|
||||||
let dx = (bottomRightGridPos.x - topLeftGridPos.x) / (heights.length - 1);
|
|
||||||
|
const dx = gridBoxWidth / (DX * (xLabels.length - 1));
|
||||||
const prevStrokeStyle = ctx.strokeStyle;
|
const prevStrokeStyle = ctx.strokeStyle;
|
||||||
ctx.strokeStyle = "red";
|
ctx.strokeStyle = "red";
|
||||||
for (let i = 0; i < heights.length; ++i) {
|
for (let i = 0; i < heights.length; ++i) {
|
||||||
@ -271,8 +263,10 @@ canvas.addEventListener(
|
|||||||
gridBoxWidth,
|
gridBoxWidth,
|
||||||
gridBoxHeight,
|
gridBoxHeight,
|
||||||
heights,
|
heights,
|
||||||
|
xLabels,
|
||||||
} = state;
|
} = state;
|
||||||
const delta = Math.ceil(gridBoxWidth / heights.length);
|
const delta = gridBoxWidth / (DX * (xLabels.length - 1));
|
||||||
|
|
||||||
const bin = Math.min(
|
const bin = Math.min(
|
||||||
Math.round(Math.max(x - topLeftGridPos.x, 0) / delta),
|
Math.round(Math.max(x - topLeftGridPos.x, 0) / delta),
|
||||||
heights.length - 1
|
heights.length - 1
|
||||||
|
Loading…
Reference in New Issue
Block a user