## How to rotate phase of wav sample at 45 degrees

Sound synthesis techniques, DSP and related mathematics

Moderators: electrogear, exonerate

### Re: How to rotate phase of wav sample at 45 degrees

treacherousumbrella wrote:Sample rate / frequency(Hz) = Number of samples per cycle
360 degrees / 45 degrees = 8
Number of samples per cycle / 8 = Delay time in samples for a 45 degree shift at the frequency of the input sinusoid

Thanks.
MegaHurtz wrote:180 degree is easy just invert :P
But a typical alpass has the same kind of 180 phase shift. So basically its a paradox :/.

Ok from 1 to 179. But now I understand how its hard to do.
Dimmak0001
essemer

Posts: 11
Joined: Fri Apr 27, 2012 7:09 pm

### Re: How to rotate phase of wav sample at 45 degrees

Warmonger wrote:Compute the offset as sampleSize * (phaseShift / 360), then just copy elementar samples in loop.

or just strat playback at offset and wrap it when it reaches sample's end, if you wnat to create a sampler.

No I dont want to create a sampler, but thanx anyway!
Dimmak0001
essemer

Posts: 11
Joined: Fri Apr 27, 2012 7:09 pm

### Re: How to rotate phase of wav sample at 45 degrees

MegaHurtz wrote:180 degree is easy just invert :P

An inversion is not a 180 degree phase shift. By sheer coincidence of the maths, it just happens that you get the same result if, and only if, the waveform is symmetrical about its zero crossings (i.e. pure sine, square, etc. oscillator.)
For a complex waveform (i.e. the kind of thing you might want to sample), they are very, very rarely the same thing.
This is why flanger/phasing effects often have an invert button - because mixing in the "upside down" version of the waveform results in different cancellations, and so a different sound.
Still a useful thing for sure, but not a phase shift.

Moving the samples backward or forward in time is the right idea, and as said it's a case of dividing up the number of samples in each cycle of the waveform. SM's stock oscillators do exactly this when you use their 'phase' inputs (the wavetables that they use are just an advanced form of sample playback).
A simple "Interpolated delay" modules is enough for this, so long as you can work out the exact amount of samples for the delay time - so the tricky part of the problem is to know the frequency of the sound.
If the frequency is known (e.g. single notes on a instrument) that is easy - otherwise, there are some nice "pitch detection" modules around on the forum that will be worth playing with.

PS) Sorry MHz if I seemed a bit harsh - not meant that way, but the invert/phase-shift thing is my particular DSP pedantry fetish!
Feel free to use any schematics and algorithms I post on the forum in your own designs - a credit is appreciated (but not a requirement).
Don't stagnate, mutate to create. Without randomness and serendipity the earth would be just another barren rock.

trogluddite
smychopath

Posts: 3028
Joined: Mon Oct 20, 2008 3:52 pm
Location: Yorkshire, UK

### Re: How to rotate phase of wav sample at 45 degrees

trogluddite wrote:...there are some nice "pitch detection" modules around on the forum that will be worth playing with.

Dimmak0001
essemer

Posts: 11
Joined: Fri Apr 27, 2012 7:09 pm

### Re: How to rotate phase of wav sample at 45 degrees

Nah man..

My conception tho.. is that it`s the same harmonics but cancelled out io added basically because the sample would disappear when overlapped. But i do mean just delay it so that the phase issue resolves in relation to the music its in. But it all depends on the stuff you are planning to do with it, for an oscillator or wavetable the options are plenty. (a reciprocal for instance). This was just for the mixing scenario.
Last edited by MegaHurtz on Sun Dec 02, 2012 8:51 am, edited 1 time in total.

MegaHurtz
smaniac

Posts: 1522
Joined: Mon Aug 11, 2008 5:29 pm
Location: Eindhoven/Netherlands

### Re: How to rotate phase of wav sample at 45 degrees

Pitch Extraction Thread - Martin's method in this thread is the most accurate that I've seen in SM. The theory is complex, but the schematic have easy to use modules that you can cut and paste, so you don't have to understand all the maths.
Trogz Toolz - this is a big toolkit of handy SM modules. Inside the "Stream, Code and ASM' section is my own take on getting audio freq etc. - much more crude than Martin's, but very low CPU use.
Feel free to use any schematics and algorithms I post on the forum in your own designs - a credit is appreciated (but not a requirement).
Don't stagnate, mutate to create. Without randomness and serendipity the earth would be just another barren rock.

trogluddite
smychopath

Posts: 3028
Joined: Mon Oct 20, 2008 3:52 pm
Location: Yorkshire, UK

### Re: How to rotate phase of wav sample at 45 degrees

...which reminds me that I have a question here too. What is the relation between delay and circular phase offset for sine waves? Picture to illustrate this - is here: viewtopic.php?f=12&t=11270&p=84834#p84834
Need to take a break? Looking for relaxing sounds? I have something right for you.
(by purchasing, you are also supporting further development of related projects).
Thank you for your contribution.
tester
smanatic

Posts: 694
Joined: Wed Jan 18, 2012 9:52 pm
Location: Poland, internet

### Re: How to rotate phase of wav sample at 45 degrees

tester wrote:What is the relation between delay and circular phase offset for sine waves?

Wavelength (samples) = Sample Rate / Frequency. (for any wave, not just sines).
...so if phase runs from zero to one (normalised)...
Delay = Phase * SR / Freq.
...or phase in radians...
Delay = Phase / (2*Pi) * SR / Freq
...or degrees...
Delay = (Phase / 360) * SR / Freq

For good accuracy be sure to use an interpolated delay, as the phase offset will be a non-integer number of samples for the vast majority of phase shifts.

In SM we mostly use normalised (zero to one) frequency values, where: Fnorm = 2 * Freq / SR.
Or, in reverse: Freq = Fnorm * 0.5 * SR.
Substituting gives the equation when using all normalised values...

Delay = Phase * SR / (Fnorm * 0.5 * SR) --> Delay = Phase * 2 / Fnorm.
Feel free to use any schematics and algorithms I post on the forum in your own designs - a credit is appreciated (but not a requirement).
Don't stagnate, mutate to create. Without randomness and serendipity the earth would be just another barren rock.

trogluddite
smychopath

Posts: 3028
Joined: Mon Oct 20, 2008 3:52 pm
Location: Yorkshire, UK

### Re: How to rotate phase of wav sample at 45 degrees

trogluddite thanks!
Dimmak0001
essemer

Posts: 11
Joined: Fri Apr 27, 2012 7:09 pm

### Re: How to rotate phase of wav sample at 45 degrees

So my point is (because my math is not so much) - on X axis, portions of circular degrees of a sine wave = linearly equal divisions of time?
Need to take a break? Looking for relaxing sounds? I have something right for you.
(by purchasing, you are also supporting further development of related projects).
Thank you for your contribution.
tester
smanatic

Posts: 694
Joined: Wed Jan 18, 2012 9:52 pm
Location: Poland, internet

### Re: How to rotate phase of wav sample at 45 degrees

Yes, it is really that simple :P

Warmonger
essemist

Posts: 173
Joined: Wed Jul 20, 2011 5:40 am
Location: Warsaw, Poland

### Re: How to rotate phase of wav sample at 45 degrees

Well - that simplifies a lot, thanks.
Need to take a break? Looking for relaxing sounds? I have something right for you.
(by purchasing, you are also supporting further development of related projects).
Thank you for your contribution.
tester
smanatic

Posts: 694
Joined: Wed Jan 18, 2012 9:52 pm
Location: Poland, internet

### Re: How to rotate phase of wav sample at 45 degrees

There's a little animation on the Wikipedia sine wave page that shows this very well.
Feel free to use any schematics and algorithms I post on the forum in your own designs - a credit is appreciated (but not a requirement).
Don't stagnate, mutate to create. Without randomness and serendipity the earth would be just another barren rock.

trogluddite
smychopath

Posts: 3028
Joined: Mon Oct 20, 2008 3:52 pm
Location: Yorkshire, UK

### Re: How to rotate phase of wav sample at 45 degrees

can't shift a sample by 45 degrees because it changes degrees 1000ds of times a second because it has multiple freqs. can only phase shift regular waves, doesnt affect their sound much. its more for dsp analysis reasons, its not a powerful synth tool in itself.

datsound
essemist

Posts: 115
Joined: Sun Sep 05, 2010 5:52 pm

Previous