DataFrame of frequencies by list comprehension?
I'm trying to build a pandas DataFrame of chromatic frequencies between A1 (55Hz) and A8 (7040Hz). 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).
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...
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.
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?
python pandas numpy list-comprehension music
add a comment |
I'm trying to build a pandas DataFrame of chromatic frequencies between A1 (55Hz) and A8 (7040Hz). 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).
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...
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.
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?
python pandas numpy list-comprehension music
BTW your octave above starts with C instead of A
– Matthias Ossadnik
Nov 9 at 21:23
add a comment |
I'm trying to build a pandas DataFrame of chromatic frequencies between A1 (55Hz) and A8 (7040Hz). 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).
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...
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.
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?
python pandas numpy list-comprehension music
I'm trying to build a pandas DataFrame of chromatic frequencies between A1 (55Hz) and A8 (7040Hz). 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).
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...
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.
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?
python pandas numpy list-comprehension music
python pandas numpy list-comprehension music
edited Nov 9 at 21:09
asked Nov 9 at 20:56
sam-la
63
63
BTW your octave above starts with C instead of A
– Matthias Ossadnik
Nov 9 at 21:23
add a comment |
BTW your octave above starts with C instead of A
– Matthias Ossadnik
Nov 9 at 21:23
BTW your octave above starts with C instead of A
– Matthias Ossadnik
Nov 9 at 21:23
BTW your octave above starts with C instead of A
– Matthias Ossadnik
Nov 9 at 21:23
add a comment |
1 Answer
1
active
oldest
votes
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. reshape
creates a view of the array content, that has shape (n_octave, 12)
such that rows are contiguous. E.g.
>>> np.arange(6).reshape((2, 3))
array([[0, 1, 2],
[3, 4, 5]])
This is just the format needed for the DataFrame.
Thank you! Could you possibly explain what exactly reshape is doing here? I'm reading the documentation but am having a hard time understanding.
– sam-la
Nov 9 at 21:44
@sam-la added explanation
– Matthias Ossadnik
Nov 9 at 22:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233168%2fdataframe-of-frequencies-by-list-comprehension%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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. reshape
creates a view of the array content, that has shape (n_octave, 12)
such that rows are contiguous. E.g.
>>> np.arange(6).reshape((2, 3))
array([[0, 1, 2],
[3, 4, 5]])
This is just the format needed for the DataFrame.
Thank you! Could you possibly explain what exactly reshape is doing here? I'm reading the documentation but am having a hard time understanding.
– sam-la
Nov 9 at 21:44
@sam-la added explanation
– Matthias Ossadnik
Nov 9 at 22:16
add a comment |
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. reshape
creates a view of the array content, that has shape (n_octave, 12)
such that rows are contiguous. E.g.
>>> np.arange(6).reshape((2, 3))
array([[0, 1, 2],
[3, 4, 5]])
This is just the format needed for the DataFrame.
Thank you! Could you possibly explain what exactly reshape is doing here? I'm reading the documentation but am having a hard time understanding.
– sam-la
Nov 9 at 21:44
@sam-la added explanation
– Matthias Ossadnik
Nov 9 at 22:16
add a comment |
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. reshape
creates a view of the array content, that has shape (n_octave, 12)
such that rows are contiguous. E.g.
>>> np.arange(6).reshape((2, 3))
array([[0, 1, 2],
[3, 4, 5]])
This is just the format needed for the 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. reshape
creates a view of the array content, that has shape (n_octave, 12)
such that rows are contiguous. E.g.
>>> np.arange(6).reshape((2, 3))
array([[0, 1, 2],
[3, 4, 5]])
This is just the format needed for the DataFrame.
edited Nov 9 at 22:16
answered Nov 9 at 21:22
Matthias Ossadnik
57427
57427
Thank you! Could you possibly explain what exactly reshape is doing here? I'm reading the documentation but am having a hard time understanding.
– sam-la
Nov 9 at 21:44
@sam-la added explanation
– Matthias Ossadnik
Nov 9 at 22:16
add a comment |
Thank you! Could you possibly explain what exactly reshape is doing here? I'm reading the documentation but am having a hard time understanding.
– sam-la
Nov 9 at 21:44
@sam-la added explanation
– Matthias Ossadnik
Nov 9 at 22:16
Thank you! Could you possibly explain what exactly reshape is doing here? I'm reading the documentation but am having a hard time understanding.
– sam-la
Nov 9 at 21:44
Thank you! Could you possibly explain what exactly reshape is doing here? I'm reading the documentation but am having a hard time understanding.
– sam-la
Nov 9 at 21:44
@sam-la added explanation
– Matthias Ossadnik
Nov 9 at 22:16
@sam-la added explanation
– Matthias Ossadnik
Nov 9 at 22:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233168%2fdataframe-of-frequencies-by-list-comprehension%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
BTW your octave above starts with C instead of A
– Matthias Ossadnik
Nov 9 at 21:23