Advertisement
LemonNing

Untitled

Apr 29th, 2024
917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
  5.  
  6. keys = {f"{notes[i % 12]}{int(i / 12)}": 440 * pow(2, (1 / 12) * (i - 57))
  7.         for i in range(9, 8 * 12 + 1)}
  8.  
  9. h_freq = 3579545
  10.  
  11. print(keys)
  12.  
  13. def note_error(note_freq: float):
  14.     x = round(h_freq / note_freq)
  15.     real_freq = h_freq / x
  16.     error_in_cent = np.log2(real_freq / note_freq) * 1200
  17.     return error_in_cent
  18.  
  19. errors = {key_name: note_error(freq) for (key_name, freq) in keys.items()}
  20.  
  21. print(errors)
  22.  
  23. plt.figure(figsize=(18, 6))
  24. plt.xticks(rotation=90)
  25. plt.xlabel("key")
  26. plt.ylabel("error in cents")
  27. plt.plot(list(errors.keys()), list(errors.values()),
  28.          marker='o', linestyle="dotted")
  29. plt.axhline(y=0.2, color='red', linestyle="dashed")
  30. plt.axhline(y=-0.2, color='red', linestyle="dashed")
  31. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement