Skip to content

Instantly share code, notes, and snippets.

@bowlercaptain
Forked from Ravarcheon/spectralRotation.py
Last active May 19, 2024 00:22
Show Gist options
  • Save bowlercaptain/75bc95e09b48037a33ac92e04e6845db to your computer and use it in GitHub Desktop.
Save bowlercaptain/75bc95e09b48037a33ac92e04e6845db to your computer and use it in GitHub Desktop.
Ravarcheon's 90-degree spectrogram rotation but with non-overwriting filenames.
import numpy as np
import soundfile as sf
from scipy.fftpack import fft, ifft
from pathlib import Path
def rotateSignal(signal,flip):
if flip:
signal = signal[::-1]
x = np.concatenate((signal, signal[1:][::-1])) # concatenating the array with a reverse of itself makes it such that the fourier transform doesn't layer over a reversed version of itself in the inverse fft
rotSig = np.real(ifft(x))
# delete the second half of the array cus fft does that thing where it also has a reversed spectrum after the regular one
rotSig = rotSig[:len(rotSig) // 2 + 1] #cheeky little plus 1 here
# this thing is quite important cus the output is usually really quiet, so this conserves the energy that the input has
energyRatio = np.sqrt(np.sum(np.abs(signal)**2) / np.sum(np.abs(rotSig)**2))
rotSig *= energyRatio
return rotSig
def skibidi(input, output,flip=True):
signal, sampleRate = sf.read(input, dtype='float64')
if signal.ndim == 1: # mono
rotSig = rotateSignal(signal,flip)
else: # stereo
rotSig = np.column_stack((rotateSignal(signal[:, 0],flip), rotateSignal(signal[:, 1],flip)))
sf.write(output, rotSig.astype(np.float64), sampleRate, format='WAV', subtype='FLOAT')
print(f"saved {output}")
# this is a 32 bit float wav, meaning that clipping isn't an issue, but DO keep in mind that you probably will get a file that has amplitudes over 0dB
if __name__ == "__main__":
inputFile = input()
fileName = Path(inputFile).stem
skibidi(inputFile, fileName+'_rotated.wav') # this is a 90 degree clockwise rotation where the beginning will be at nyquist frequency and the end be at 0hz
swappedName = fileName+'_swapped.wav'
skibidi(inputFile, swappedName,False) # this is a swap of the time and frequency axiis, this time the end of the sound is at nyquist and the beginning is at 0hz
skibidi(swappedName, fileName+'_inverted.wav') # this is a vertical flip of the input signal and reversed
# a 180 degree rotation can be achieved simply by reversing inverted.wav
# a 270 degree rotation can be achieved simply by reversing swapped.wav
skibidi(swappedName, fileName+'_restored.wav',False) # for comparing loss in reconstruction if you want, delete this line if you dont care about that lol
#i make music too so please check that out lolol https://ravarcheon.fanlink.tv/profiles https://linktr.ee/ravarcheon https://www.youtube.com/ravarcheon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment