简体   繁体   English

列表理解的频率数据帧?

[英]DataFrame of frequencies by list comprehension?

I'm trying to build a pandas DataFrame of chromatic frequencies between A1 (55Hz) and A8 (7040Hz).我正在尝试在 A1 (55Hz) 和 A8 (7040Hz) 之间构建一个半音频率的 Pandas DataFrame。 Essentially, I want it to look like this...本质上,我希望它看起来像这样......

df = pd.DataFrame(columns=['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'])
df.loc[0] = (55, 58.27, 61.74, 32.7, 34.65, 36.71, 38.89, 41.2, 43.65, 49, 51.91)

But without having to manually assign all the frequencies to their respective notes and with an octave per row (octave 1 to 8).但是不必手动将所有频率分配给它们各自的音符,并且每行一个八度(八度 1 到 8)。

Based on the site http://pages.mtu.edu/~suits/notefreqs.html , the space between each note (or a 'half-step') given a single note is...根据网站http://pages.mtu.edu/~suits/notefreqs.html ,给定单个音符的每个音符(或“半步”)之间的空间是...

def hz_stepper(fixed_note, steps):
    a = 2 ** (1/12)
    return fixed_note * a ** steps

Using that function 'hz_stepper', I can chromatically increase or decrease a given note n times by assigning 1 or -1 to steps variable.使用该函数“hz_stepper”,我可以通过将 1 或 -1 分配给 step 变量来对给定的音符进行 n 次半音增加或减少。

My question is, how do I create a DataFrame where all the rows look like how I did it manually, but using a list comprehension to form the rows?我的问题是,如何创建一个 DataFrame,其中所有行看起来都像我手动完成的那样,但是使用列表理解来形成行?

just iterate over the pitches and reshape the result afterwards:只需迭代音高并在之后重塑结果:

import numpy as np
import pandas as pd

base = 55.
n_octave = 8
columns = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#']

factors = 2**(np.arange(12 * n_octave) / 12.)
pd.DataFrame(data=base * factors.reshape((n_octave, 12)), columns=columns)

Explanation解释

factors are the desired frequencies as 1d numpy array, but they are not in the tabular form required for the DataFrame. factors是 1d numpy 数组所需的频率,但它们不是 DataFrame 所需的表格形式。 reshape creates a view of the array content, that has shape (n_octave, 12) such that rows are contiguous. reshape创建数组内容的视图,其形状为(n_octave, 12) ,因此行是连续的。 Eg例如

>>> np.arange(6).reshape((2, 3))
array([[0, 1, 2],
       [3, 4, 5]])

This is just the format needed for the DataFrame.这只是 DataFrame 所需的格式。

from your begining :从你开始:

df = pd.DataFrame(columns=['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'])
df.loc[0] = 55*2**(np.arange(12)/12)
for i in range(8): df.loc[i+1]=2*df.loc[i]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM