Skip to content

Instantly share code, notes, and snippets.

@francescopapaleo
Created November 12, 2024 08:56
Show Gist options
  • Select an option

  • Save francescopapaleo/aeec4b3a4a7dd076dc6641ed6880bb7b to your computer and use it in GitHub Desktop.

Select an option

Save francescopapaleo/aeec4b3a4a7dd076dc6641ed6880bb7b to your computer and use it in GitHub Desktop.
Lambda functions with Torch for basic DSP conversions
import torch
torch.pi = torch.acos(torch.zeros(1)).item() * 2 # which is 3.1415927410125732
db_to_mag = lambda x: 10 ** (x / 20)
mag_to_db = lambda x: 20 * torch.log10(x)
normalize = lambda x: x / torch.max(torch.abs(x), dim=1, keepdim=True)[0]
midi_to_hz = lambda x: 440 * (2 ** ((x - 69) / 12))
hz_to_midi = lambda x: 12 * torch.log2(x / 440) + 69
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment