[英]How can I add each element of fourier.getEnergy() into an array?
Trying to make a visualizer, the values in fft.getEnergy() (a p5.js sound library function) returns the amount of volume at each frequency.试图制作一个可视化工具,fft.getEnergy()(一个 p5.js 声音库函数)中的值返回每个频率的音量。 I would like to take the first 1024 of these volumes and store them in an array so I can map each number to a graph.
我想取出这些卷中的前 1024 个并将它们存储在一个数组中,这样我就可以将每个数字 map 放到一个图表中。 The problem is when I try to add them to the array, the array stores only 1 value 1024 times and not the first 1024 different values.
问题是当我尝试将它们添加到数组时,数组仅存储 1 个值 1024 次,而不是前 1024 个不同的值。
This is a snapshot of the getEnergy() values: https://imgur.com/a/7zSV9DK这是 getEnergy() 值的快照: https://imgur.com/a/7zSV9DK
And this is a snapshot of the array I'm trying to make while trying to push the first 1024 values: https://imgur.com/a/cs3FA0U which shows that it's printing one value 1024 times.这是我在尝试推送前 1024 个值时尝试制作的数组的快照: https://imgur.com/a/cs3FA0U这表明它正在打印一个值 1024 次。
var arrays = []
for(var i = 0; i < 1024; i++)
{
arrays.push(energy) //energy = fft.getEnergy("bass")
}
console.log(arrays)
From what I read on this page https://p5js.org/reference/#/p5.FFT/getEnergy根据我在此页面上阅读的内容https://p5js.org/reference/#/p5.FFT/getEnergy
getEnergy("bass")
return a "predefined frequency ranges" which I guess is an array of energies of predefined frequencies from 0
to bass max limit
. getEnergy("bass")
返回一个“预定义的频率范围”,我猜它是从0
到bass max limit
的预定义频率的能量数组。
What you do in your for
loop is pushing the result array of frequencies to your result array 1024 times (an not 1024 individual values as you want).您在
for
循环中所做的是将频率结果数组推送到结果数组 1024 次(而不是您想要的 1024 个单独的值)。
so what your are trying to do is所以你想要做的是
const resultArray = [];
for (let i=0; i<1024; i++) {
resultArray.push(energy[i]);
}
console.log(resultArray);
(note that I don't use var
. I Suggest that you look at the difference between var | let | const
and that you learn about the notion of scope too.) (请注意,我不使用
var
。我建议您查看var | let | const
之间的区别,并且您也了解 scope 的概念。)
Yet this code above is not quite good either.然而,上面的这段代码也不是很好。 I suggest you look at what the
Array
object in JS is and look at all the ways to manipulate it.我建议你看看 JS 中的
Array
object 是什么,并查看所有操作它的方法。 Using the slice
method would be better.使用
slice
方法会更好。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
const resultArray = energy.slice(1024);
And then to sort your array you can use the sort
method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort然后对数组进行排序,您可以使用
sort
方法https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
(Though be careful that "the default sort order is ascending, built upon converting the elements into strings") (虽然要注意“默认排序顺序是升序,建立在将元素转换为字符串的基础上”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.