# How would I spectrally rotate/invert an audio signal (Matlab or Python)?

So I have a 4.5 minute wav audio clip. I want to spectrally rotate the frequencies such that the spatio-temporal characteristics of natural speech are retained, while having the new audio be unintelligible. Any tips? I found https://ccrma.stanford.edu/~jos/sasp/Spectral_Rotation_Real_Signals.html, but I'm not getting anywhere with it.

• Spectral inversion (Swapping high and low frequencies) is simple to do and I can show you how to do that if that is indeed what you are interested in, but know that it is simplistic covertness that is easy to detect and easily corrected for so has no practical purpose except for unsophisticated applications. This was done in the days prior to digital processing where now we can much more easily simply scramble the data payload. That said, if this is what you want please clarify and I will add it as an answer. – Dan Boschen Jul 29 at 19:12
• Yeah that's exactly what I need, swapping high and low (the practical purpose is to act as a control in a research study).. So I currently have it as x(n) domain (so after you import it into matlab using audioread if that helps) – Armis Jul 29 at 19:34
• See this and let us know if you have specific further questions not answered here: windytan.com/2013/05/…. (Specifically multiply every other sample by -1----which will swap DC with Fs--- but you if you are oversampled you may want to do further processing to limit to a sub-band in your digital spectrum... – Dan Boschen Jul 29 at 19:37
• @endolith no need to take an fft. What Hilmar wrote is much simpler, no? The assumption was the waveform is in the time domain. – Dan Boschen Jul 29 at 21:01
• Yeah, Looking back, I think what Hilmar said makes more sense.... I tried that out and found out that in time domain, the spatial-temporal characteristics (both original and inverted look the same) are the same. But in frequency domain, the power spectra are both different- which is what I was intended to do. When I play the new inverted signal, it seems I do get what I want- just that the audio seems to be a bit low- so maybe i'll have to boost it up a bit so the research subjects can hear it – Armis Jul 30 at 13:39

After reading the linked article by Julius Smith, flipping the spectrum entirely as the OP is requesting is NOT the intention of that author. That article is shifting the spectrum to the left by only half a DFT bin as an alternate approach to the complexity of creating an analytic signal with the Hilbert Transform for purposes of making an octave band filter on a real signal (per one post prior). It is eliminating the samples at DC and Nyquist and converting the real signal into a complex signal.

Specifically, if you multiply a discrete signal in the time domain by a complex phasor of the general form $$e^{j\omega n}$$ where n is the sample count and $$\omega$$ is a fractional radian frequency between $$0$$ and the sample rate given as $$2\pi$$, the spectrum will be shifted accordingly by $$\omega$$ as given by the frequency translation property of the FT (when $$\omega$$ is a positive number this shift will be to the right, and when $$\omega$$ is a negative number this shift will be to the left). Since the frequency spectrum for a discrete signal is periodic over the fractional radian frequency range from $$0$$ to $$2\pi$$, this shift will also be a spectral rotation, as is clearer from the spectrums I plotted further below (consider shifting these spectrums a small amount to the left and observe how within the range from $$0$$ to $$2\pi$$ the spectrum rotates meaning that the spectrum that goes over $$f_s$$ in the shift appears at $$f=0$$.)

Julius Smith is shifting the spectrum to the left by only half a DFT bin (a very small shift), specifically by doing the following:

$$y[n] = e^{-\pi n/N} x[n]$$

Note how the DFT formula for the DFT:

$$X[k] = \sum_{n=0}^{N-1} x[n] e^{2 \pi nk/N}$$

Where $$x[n]$$ is rotated using integer phase steps given by $$k$$ in $$e^{2 \pi k n/N}$$, so Julius approach results in a shift of $$k/2$$.

That said, if the OP indeed wants to flip the spectrum entirely (swap low and high frequency), this is then done by shifting the spectrum entirely over $$N/2$$ bins (half the entire DFT spectrum, not just one bin), which ends up being, using the same form but with using $$k=N/2$$ instead of $$k=1/2$$ as Julius Smith had done,

$$y[n] = e^{2\pi(N/2) n/N} x[n] = e^{\pi n} x[n] = (-1)^n x[n]$$

To further see how this works and how the spectrum in discrete systems is circular, see the plots demonstrated below. Given any spectrum in the unique span of $$f \in [0, f_s)$$ where $$f_s$$ represents the sampling rate, or equivalently and more frequency used in fractional radian frequency $$f\ in [0, 2\pi )$$, we can circularly shift in the frequency domain by multiplying the signal in the time domain by $$e^{j\omega_s n}$$ where $$\omega_n$$ represents the fractional radian frequency amount to shift.

This applies to all signal types, meaning real and complex so is the more general case. For real signals the unique span is $$f \in [0, f_s/2)$$ or in fractional radian frequency $$f \in [0, \pi)$$. So this is the OP's case with an audio file representing a real signal. The OP wants to rotate the spectrum such that $$f=0$$ is translated to $$f= \pi$$ (Equivalently half the sampling rate or $$f_s/2$$.)

However if the signal is over-sampled such that the spectrum of interest does not fully occupy the frequency range of $$0$$ to $$f_s/2$$ then this process will be unsatisfactory as demonstrated below.

The top spectrum shows the case where the audo spectrum occupies the full range from $$0$$ to $$fs/2$$, showing the "unfolded spectrum with the frequency axis extending to $$\pm \infty$$ which nicely reveals the frequency periodicity mentioned above. The next line is the spectrum of the sequence +1,-1,+1,-1... specifically being a tone that is sampled at exactly one half the sampling rate. When we multiply in time, we convolve in frequency, resulting in the third line of the spectrum shifted by $$f_s/2$$.

When the signal is oversampled, the same process would result in the following spectrums, which may not be what the OP is hoping to achieve. In this case a suggested approach is to decimate the signal so that $$f_s/2$$ is at the upper end of the desired spectrum and follow the process above. If for rate matching the higher sampling rate is needed, the signal can be again interpolated after the multiplication by +1,-1,+1,-1...

$$y[n] = (-1)^n \cdot x[n]$$

This will flip the spectrum in linear frequency. Easy to do, easy to recover.