将Javascript映射函数转换为Swift 2映射函数
我重写这个问题是因为我的第一个问题很模糊。我正在尝试使用map函数将下面的javascript函数转换为Swift 2 下面是javascript函数将Javascript映射函数转换为Swift 2映射函数,javascript,swift,Javascript,Swift,我重写这个问题是因为我的第一个问题很模糊。我正在尝试使用map函数将下面的javascript函数转换为Swift 2 下面是javascript函数 function compute_correlations(timeseries, test_frequencies, sample_rate) { // 2pi * frequency gives the appropriate period to sine. // timeseries index / sample_rate
function compute_correlations(timeseries, test_frequencies, sample_rate)
{
// 2pi * frequency gives the appropriate period to sine.
// timeseries index / sample_rate gives the appropriate time coordinate.
var scale_factor = 2 * Math.PI / sample_rate;
var amplitudes = test_frequencies.map
(
function(f)
{
var frequency = f.frequency;
// Represent a complex number as a length-2 array [ real, imaginary ].
var accumulator = [ 0, 0 ];
for (var t = 0; t < timeseries.length; t++)
{
accumulator[0] += timeseries[t] * Math.cos(scale_factor * frequency * t);
accumulator[1] += timeseries[t] * Math.sin(scale_factor * frequency * t);
}
return accumulator;
}
);
return amplitudes;
}
这里是我填充测试频率的地方
for (var i = 0; i < 30; i++)
{
let note_frequency = C2 * pow(2.0, Double(i) / 12.0)
let note_name = notes[i % 12]
let note = NoteInfo(theFrequency: note_frequency, theNoteName: note_name)
test_frequencies.append(note)
}
for(变量i=0;i<30;i++)
{
请注意_频率=C2*pow(2.0,双(i)/12.0)
let note_name=notes[i%12]
let note=NoteInfo(频率:note\u频率,可能名称:note\u名称)
测试频率。附加(注)
}
您的累加器是一个[Double]
,因此您的映射的结果变成[[Double]]
。然后尝试将其分配给[Double]
您应该相应地声明振幅:
let amplitudes: [[Double]] = test_frequencies.map { f in
或者(根据您的需要)只返回映射中的一个累加器字段,例如
return accumulator[0]
那么你有什么问题吗?听起来像是test\u frequencies
是一个Double的集合。
我将在填充test\u frequencies的地方添加代码。好的,那么振幅是一个Double数组,这是语法的意思吗?这似乎消除了错误。现在,对于代码的其余部分。。。试图使吉他调谐器,所以谁知道它会如何,但这修复了错误,所以我接受作为正确的。
let amplitudes: [[Double]] = test_frequencies.map { f in
return accumulator[0]