英文:
How to come from equations to code with javascript
问题
我将首先用JavaScript实现这四个方程,然后再用Go实现。我的问题是,我不是数学家,不知道如何阅读这些方程。
第一个方程是FFT窗口Blackman。
第二个方程是FFT算法。
第三个方程是时间平滑。
第四个方程是转换为分贝。
我不能使用FFT库来完成这个任务,必须实现这四个方程。当工作完成后,我将按照相同的顺序在相同的信号上运行这四个方程。
有人可以帮忙解释如何从方程转化为可工作的代码吗?我不知道从哪里开始。
谢谢每一个回答。
英文:
I will implement four equations first in javascript and later in go. My problem is i'm not a Mathematician, i don't know how to read the equations.
First one is the FFT window Blackman.
Second is an FFT algorithm:
Third is Smoothing over time:
Fourth is to decibel:
I don't can use a fft library for this job, it's required to implement this four equations. When the work is done i will run this four equations in this sequence on the same signal.
Can anyone help and explain how i come from the equations to working code?
I don't know where i should start.
Thank you for each answer
答案1
得分: 1
这是JavaScript代码。对于Go语言,请进行相应的更改。我没有测试所有内容,只是将数学方程转换为代码。如果有错误,请纠正答案。
var pi = 3.14;
var blackmann = function(N){
var a = 0.16;
var a0 = (1-a)/2, a1 = 1/2, a2 = a/2;
var w = [];
for (var i=0; i<N; i++){
w[i] = a0 - a1*Math.cos(2*pi*i/N) - a2*Math.cos(4*pi*i/N);
}
return w;
}
var fft = function(x, K){
var X1 = [], X2 = [];
var N = x.length;
// X1 for the real spectrum, X2 for the imaginary part.
// For magnitude spectrum take |X1^2 + X2^2|
// For a K point fft
for (var k=0; k<K; k++){
for (var n=0; n<N; n++){
X1[k] = Math.cos(2*pi*n/N);
X2[k] = Math.sin(-2*pi*n/N);
}
}
return {"real":X1, "img":X2}
}
var smooting = function(x){
var s = [], t = 0.16;
s[0] = x[0];
for (var i=1; i<x.length; i++){
s[i] = t*s[i-1] + (1-t)*x[i];
}
return s;
}
var decibel = function(X){
var Y = [];
for (var i =0; i<X.length; i++){
Y[i] = Math.log10(Math.abs(X[i]));
}
return Y;
}
以上是翻译好的代码部分。
英文:
This is in javascript. Make the appropriate changes for Go as well. I've not tested everything, just translated the math equations into code. If there are errors please correct the answer.
var pi = 3.14;
var blackmann = function(N){
var a = 0.16;
var a0 = (1-a)/2, a1 = 1/2, a2 = a/2;
var w = [];
for (var i=0; i<N, i++){
w[i] = a0 - a1*Math.cos(2*pi*i/N) - a2*Math.cos(4*pi*i/N);
}
return w;
}
var fft = function(x, K){
var X1 = [], X2 = [];
var N = x.length;
// X1 for the real spectrum, X2 for the imaginary part.
// For magnitude spectrum take |X1^2 + X2^2|
// For a K point fft
for (var k=0; k<K; k++){
for (var n=0; n<N; n++){
X1[k] = Math.cos(2*pi*n/N);
X2[k] = Math.sin(-2*pi*n/N);
}
}
return {"real":X1, "img":X2}
}
var smooting = function(x){
var s = [], t = 0.16
s[0] = x[0];
for (var i=1; i<x.length; i++){
s = t*s[i-1] + (1-t)*x[i]
}
return s
}
var decibel = function(X){
var Y = [];
for (var i =0;i<X.length(); i++){
Y[i] = Math.log10(Math.abs(X[i]));
}
return Y;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论