UK Vintage Radio Repair and Restoration Powered By Google Custom Search Vintage Radio and TV Service Data

Go Back   UK Vintage Radio Repair and Restoration Discussion Forum > General Vintage Technology > Components and Circuits

Notices

Components and Circuits For discussions about component types, alternatives and availability, circuit configurations and modifications etc. Discussions here should be of a general nature and not about specific sets.

Closed Thread
 
Thread Tools
Old 16th Jun 2018, 8:07 pm   #1
julie_m
Dekatron
 
Join Date: May 2008
Location: Derby, UK.
Posts: 7,735
Default Heavy maths? Sample rates and pitch shifting

I wondered if someone a bit mathematically-inclined might be able to give me a bit of advice?

Say I have a .wav file and I want to alter the pitch by inserting additional samples between existing ones to reduce it, or removing samples to increase it. There is already software out there that does this, but I'm wanting to vary the sample rate in accordance with a function that varies with time. So I might as well do all the maths myself. Obviously I could just duplicate the previous value when shifting down and discard samples when shifting up, but that would introduce distortion as the waveform would now have jagged bits in it.

Consider the samples in the file as y values for a series of x values implied by the sample number and the time difference Δt = 1/r between samples. Now, if I have N points, I can always construct a polynomial of order N-1 that passes through them all, and doesn't do anything surprising between any one and the next. That's exactly as good as the original sample file as a description of the waveform, though potentially four times the size as each of the coefficients in the equation is a 64-bit floating point value as opposed to a 16-bit integer in the original file. Now I can solve N simultaneous equations and so determining the coefficients of the polynomial. Then I just have to plug in the x values corresponding to the new sample rate and calculate the corresponding y values.

Doing this for several minutes of audio probably is going to need more computing power than I can muster, though.

However, I could compromise by using a sliding window, and looking at only part of the waveform at any one time. The approximation should hold well enough within the window to find as many new samples as the new sampling rate permits, then I just move the window and work on the next portion of the waveform. In the degenerate case, where the window is only two points wide, I would be interpolating linearly between pairs of points on the waveform to get the new sample values. With a window three samples wide, I would get a quadratic equation approximating the waveform; four samples, a cubic; and so forth. The approximation gets better with more points, but the computation gets more intensive.

Is this going to work? And what's a sensible size window to be using, before diminishing returns set in and the increase in computational complexity from adding more points begins to outweigh the resulting increase in accuracy?

Or ..... is there a nice Open Source C library that does most of the maths already?
__________________
If I have seen further than others, it is because I was standing on a pile of failed experiments.
julie_m is offline  
Old 16th Jun 2018, 10:34 pm   #2
kalee20
Dekatron
 
Join Date: Feb 2007
Location: Lynton, N. Devon, UK.
Posts: 7,061
Default Re: Heavy maths? Sample rates and pitch shifting

I'd disagree that between N points, it is possible to construct a polynomial... which doesn't do anything surprising between them. Although the polynomial will pass through the given points, it could go wild between them!

Gut feeling is to take the points, at most, four at a time and use a cubic to interpolate between them. Extrapolating (to generate the samples between one set of four and the next set of four) should also be pretty good.
kalee20 is offline  
Old 16th Jun 2018, 10:40 pm   #3
Radio Wrangler
Moderator
 
Radio Wrangler's Avatar
 
Join Date: Mar 2012
Location: Fife, Scotland, UK.
Posts: 22,801
Default Re: Heavy maths? Sample rates and pitch shifting

The usual method is to increase the sampling rate by simply resampling at the new rate... you just stuff zero values when an extra bit of data is needed, or to decrease the sampling rate, just resample at the new rate it will automatically miss an incoming sample when needed. But with both processes the signal is then passed through an anti-aliasing filter. It is this filter which performs the necessary interpolation. No need for polynomial calculations, just a routine FIR filter does the job.

David
__________________
Can't afford the volcanic island yet, but the plans for my monorail and the goons' uniforms are done
Radio Wrangler is offline  
Old 16th Jun 2018, 11:44 pm   #4
PJL
Dekatron
 
Join Date: Sep 2005
Location: Seaford, East Sussex, UK.
Posts: 5,997
Default Re: Heavy maths? Sample rates and pitch shifting

Linear interpolation should only need to look at the last and next values.
PJL is offline  
Old 16th Jun 2018, 11:52 pm   #5
kevinaston1
Hexode
 
Join Date: Aug 2014
Location: Featherstone, West Yorkshire, UK.
Posts: 386
Default Re: Heavy maths? Sample rates and pitch shifting

Download a copy of Audacity (freeware).

Once the song has loaded, select the whole track, select effect, select pitch shift, and finally select how much shift you desire - done.


Kevin
kevinaston1 is offline  
Old 17th Jun 2018, 7:59 am   #6
Radio Wrangler
Moderator
 
Radio Wrangler's Avatar
 
Join Date: Mar 2012
Location: Fife, Scotland, UK.
Posts: 22,801
Default Re: Heavy maths? Sample rates and pitch shifting

Quote:
Originally Posted by PJL View Post
Linear interpolation should only need to look at the last and next values.
True, but simple linear interpolation isn't the only form of interpolation and curve-fitting ones produce better results.

Changing the sampling rate is a sampling process and stirs up the aliasing business business all over again.Viewed in the frequency domain, an anti-alias filter is beneficial. Viewed in the time domain, it gives a higher order curve fit interpolation.

David
__________________
Can't afford the volcanic island yet, but the plans for my monorail and the goons' uniforms are done
Radio Wrangler is offline  
Old 18th Jun 2018, 4:39 pm   #7
dave cox
Nonode
 
dave cox's Avatar
 
Join Date: Jan 2009
Location: Bristol, UK.
Posts: 2,059
Default Re: Heavy maths? Sample rates and pitch shifting

Actually, I think it could be done with 4 points fitted to a cubic polynomial.

The complication you have stated is that you want to dynamically change the 'sample interval' and then convert the result back into a fixed 'sample interval' that a normal DAC can actually play back! To compute each output sample, find the 4 input samples that bracket it (allowing for the modulated timebase), fit the polynomial, calculate the the output point given its time offset. You then need to solve 1 cubic for each output data point - that sounds easily within the realms of anything from the last 10 years!

dc
dave cox is offline  
Old 18th Jun 2018, 8:02 pm   #8
julie_m
Dekatron
 
Join Date: May 2008
Location: Derby, UK.
Posts: 7,735
Default Re: Heavy maths? Sample rates and pitch shifting

Quote:
Originally Posted by dave cox View Post
Actually, I think it could be done with 4 points fitted to a cubic polynomial.
I didn't think 4 points would be nearly enough -- I was thinking of maybe 32 or 256 points. Still, anything that makes the job easier has to be a good thing, right?

Quote:
The complication you have stated is that you want to dynamically change the 'sample interval' and then convert the result back into a fixed 'sample interval' that a normal DAC can actually play back! To compute each output sample, find the 4 input samples that bracket it (allowing for the modulated timebase), fit the polynomial, calculate the the output point given its time offset. You then need to solve 1 cubic for each output data point - that sounds easily within the realms of anything from the last 10 years!
Yes, getting the x values for the new samples is going to be nearly as much fun as calculating the y values. But I'm one of those maths perverts that gets a kick out of numerical approximation
__________________
If I have seen further than others, it is because I was standing on a pile of failed experiments.
julie_m is offline  
Old 18th Jun 2018, 9:37 pm   #9
mark_in_manc
Octode
 
Join Date: Nov 2011
Location: Manchester, UK.
Posts: 1,872
Default Re: Heavy maths? Sample rates and pitch shifting

From memory, I think I would once have done this as David suggests and let the anti-aliasing sort out the unwanted HF components that 'jagged bits' in the time history imply. I wonder if you need to move the aliasing filter around, or whether you can just run it at your lowest effective f_s/2, and lose a bit of audio bandwidth that you could have enjoyed in those periods where f_s went up a bit. Nth order low-pass FIR filters are definitely a library function in Matlab, though that's not a bit of free ware - and anyway, if you're a math geek you probably want to write your own convolution routines and implement it all in the time domain by brute force

(Edit to add - I was reading about SSB the other night; some ideas there are familiar, others less so. If anyone wants to attempt a 'Hilbert transform for dummies' explanation here or in its own thread I would be rather interested!)
mark_in_manc is offline  
Old 18th Jun 2018, 11:10 pm   #10
Radio Wrangler
Moderator
 
Radio Wrangler's Avatar
 
Join Date: Mar 2012
Location: Fife, Scotland, UK.
Posts: 22,801
Default Re: Heavy maths? Sample rates and pitch shifting

I can, but not just now. About to pack in for the day. The Hilbert transform AND quadrature down conversion can be packed into a pair of lowpass filters and at the same time can vanish half the filters as well as slowing the clock of what remains down to a quarter of the rate you first thought of. I did it from first principles about 20 years ago, and it's only recently gone out of production. Definitely works!

For filter design sans Matlab, pick a filter, compute its impulse respunse, sample that impulse response, and voila! you have the coefficients. Proof is trivial. Input a unit impulse to an FIR filter and it plays the coefficients one at a time in sequence.

David
__________________
Can't afford the volcanic island yet, but the plans for my monorail and the goons' uniforms are done
Radio Wrangler is offline  
Old 18th Jun 2018, 11:36 pm   #11
GMB
Dekatron
 
GMB's Avatar
 
Join Date: Aug 2003
Location: near Reading (and sometimes Torquay)
Posts: 3,086
Default Re: Heavy maths? Sample rates and pitch shifting

What exactly is the intent?

If you keep adding extra samples then this both reduces the pitch and slows it down. To just shift the pitch alone I would have thought you would have to do a Fourier transform, shuffle the frequency domain data and then reverse transform to get a pitch shifted version in the same timescale.
GMB is offline  
Old 19th Jun 2018, 7:19 am   #12
Radio Wrangler
Moderator
 
Radio Wrangler's Avatar
 
Join Date: Mar 2012
Location: Fife, Scotland, UK.
Posts: 22,801
Default Re: Heavy maths? Sample rates and pitch shifting

Mark, The Hilbert transform functions as a phase shifter/splitter which operates over a useful bandwidth. Implemented digitally, the sampling rate sets limit on upper frequency (or offset frequency when a Nyquist alias is used), and the storage length sets a limit on the lower ditto. In practice, it can be implemented as an FIR filter with the tap coefficients transformed. It doesn't necessarily add any computational burden if you were going to filter anyway. You could build an analogue version using a delay-line type transversal filter.

Let's consider an example application:

Almost all of the processes in the 'analogue world' are matched by analogous processes in the 'digital world' and vice versa. What are different are the price tags, the difficulty, and the limits on performance. So the optimum choices of processes can be very different in the two worlds.

Say you want to make an analogue single-sideband receiver. The classic method is to down convert the signal to a convenient and fixed IF, then to use a narrow filter (often quartz crystal-based) to select the wanted sideband, and then mix the signal with a carrier insertion oscillator's frequency. Lowpass filter the mixer's output, amplify to taste and apply to a speaker or headphones. This has been developed to quite high performance. But crystal filters are expensive and getting more so.

There is an alternative way, the "phasing method". Split your signal (IF or RF have both been done) and apply to two identical mixers. Apply the same local oscillator frequency to each mixer, at the frequency the carrier should have been if it hadn't been suppressed. BUT apply the LO to the mixers with a 90 degree phase shift. Maybe use an L-C network to do the shift, or else take shifted outputs from a frequency divider.

You now have two IFs, ostensibly the same, but 90 degrees to each other. Mixers are analogue multiplirers. When you multiply vectors in polar form, you multiply moduli and add arguments. So the 90 degree shift in the LOs gets added into all the signal components.

The mix down has placed the carrier frequency at zero hertz. What was the upper sideband is now audio at positive frequencies. What was the lower sideband is now audio but at negative frequencies. If we only had one signal path we wouldn't be able to ever separate the two sidebands, the negative frequencies of the LSB would be folded over and added to the USB. We'd hear both at once and we'd have a DSB receiver. If our LO was even a little off frequency, the USB and LSB basebands would not be perfectly aligned so AM would sound very nasty and if we were trying to receive SSB, other signals would intrude.

But we don't have only one signal path, we have two. AND they are 90 degrees shifted with respect to each other. That gives us a perspective view of them and we CAN tell apart vectors whirling in one direction from those at the same speed in the opposite direction.

If I gave you a graph of the height of a bicycle pedal versus time you could deduce the height of the opposite pedal and plot it. You could tell me the speed of pedalling, but you could not tell me the direction. The cyclist could be freewheeling and back-pedaling. If I fitted the cranks on the bike at any angle other than 0 and 180 degrees (90 would be optimal) and I graphed them both, and told you which was which, you could now tell me the direction of rotation. Bicycles from the isle of man have three pedals at 120 degrees and they too give out-of-plane perspective). Single phase induction motors can run either way and need a phase shifter to give them direction to start.

Back to the SSB receiver:

If I take the outputs of the two mixers and lowpass filter each of them (passing the wanted audio range) I've done the job which would have taken a bandpass filter before the mixers.

If I now make an audio frequency phase shifting network, I can 90 degree shift one of my audio paths, and then if I add my two audio paths together, I get cancellation of either USB or LSB inputs. Which depends on the signs of the phase shifts. If I want the other sideband, I only need to invert either path or either LO.

This is a lot cheaper than crystal filters.

BUT it is a cancellation process, and the depth of cancellation requires very good matching of gains and phases in the two channels. It was the poor man's way of doing SSB. Cheaper, but of limited performance.

Now let's do it digitally:

We'll down convert our signal to a suitable IF, if needed. Then we shove it into an ADC (not forgetting an anti alias filter). Let's set the sampling frequency at four times the carrier frequency. The first necessary process is to put the signal into a pair of multipliers (mixers) with enough bit width to handle the dynamic range of our receiver and we require a quadrature pair of LOs.

Now, as the signal exists as samples, we only need the LOs at those sample times AND we need four samples per cycle. Nothing forces me to choose any particular starting phase, so I can choose something easy! 0 degrees is easy. My sampled LO sinewave becomes a numerical sequence of 0,1,0,-1 repeating and the 90 degree shifted (cosine) LO becomes a sequence of 1,0,-1,0 repeating.

This is rather convenient. Half my samples get multiplied by zero, which means they take no further work to handle, but I need to keep their positions for the time being. The rest of the samples get multiplied either by 1 or -1. So those digital multipliers just got very easy.

We'll call our two paths I and Q just for tradition. Both I and Q need lowpass filtering. Let's give them a nice simple FIR filter each. Each FIR iis a tapped shift register delay line. Each tap gets multiplied by an individual coefficient (to set the filter shape) Then all the I products are added together to make the filtered I path, all the Q products are added similarly.

As half my samples into these filters are zero, it would be pointless to waste computation on them. Also me baseband has significantly lower frequency content than the digitised IF had. I can reduce my sampling rate. The process is called 'decimation' though the factor is rarely 10 and the Romans killed 1 in 10, not 9 in 10.

Let's split our samples up into 4 paths, each path running at 1/4 of our original .sample rate.
so i and Q with four paths each. 8 paths total. A distributor sends incoming samples to I1, I2, I3, I4 in sequence. A second distributor sends the same samples to Q1, Q2, Q3 and Q4 in sequence. Now I and Q only differ in the LO sequence which is a 4 step cycle, and the distributor is a 4 step cycle running at the same rate. the two cycles are in step. All the x1 multiplicands from the I multiplier go into the same filter chain, all the x-1 multiplicands go into the other, and the remaining two chains get nothing but zeros! The same happens in Q, but the shifted 1, 0, -1, 0 means the Q chains getting zeros and those getting data are swapped.

Hey, we not only slowed the rte our digital filters have to run at, we just made half of them vanish!

Because the distributor syncs with the LOs, and the -1 multiplicands go into different chains to the +1 multiplicands, I can dump the digital multipliers on the front end and I can get the same effect by inverting the signs of the coefficients on two of the chains. NOW both distributors are doing the same thing to the same signal, so I only need one.

I now have only 4 chains, one chain gets each tap multiplied by the appropriate coefficient, another gets each tap multiplied by its inverted coefficient, and all the products get summed to create the filtered I datastream, now sampled at a quarter of the ADC rate. The two other chains similarly make the ongoing Q datastream.

If wanted, we can filter furter and decimate further.

We have done the 90 degree LO phase shifter AND the downconversion mixers just by mucking about with a couple of FIR filters. Additionally we made a load of hardware vanish and we made it go slower.

Phew.

A similar process can lift the frequency up and down again to do the audio phase shift to complete the SSB receiver (look up 'Third method SSB')

Making bandpass filters in DSP is inefficient, So it's common to do this to use a pair of lowpass filters instead, and then convert back up if necessary.

So a Hilbert transformer amounts to a pair of digital filters wit a game played on their coefficients.

Typing fingers both now sore!

David
__________________
Can't afford the volcanic island yet, but the plans for my monorail and the goons' uniforms are done
Radio Wrangler is offline  
Old 19th Jun 2018, 8:57 am   #13
mark_in_manc
Octode
 
Join Date: Nov 2011
Location: Manchester, UK.
Posts: 1,872
Default Re: Heavy maths? Sample rates and pitch shifting

Rather like your first, this is a holding message to say 'I will have time to do justice to your post between 6 and 8pm tonight' I think it will take me that long - though there might be some commonality with something I once knew about MPEG audio compression and making sub-bands whilst maintaining overall sample rate. It's been a while!
mark_in_manc is offline  
Old 19th Jun 2018, 12:58 pm   #14
kalee20
Dekatron
 
Join Date: Feb 2007
Location: Lynton, N. Devon, UK.
Posts: 7,061
Default Re: Heavy maths? Sample rates and pitch shifting

Quote:
Originally Posted by Radio Wrangler View Post
Typing fingers both now sore!

David
I've got no practical interest in the OP's problem, just a mental interest in things maths-ey - but I have to say that is a really clear explanation of resolving SSB using digital techniques. It has added to my education. Thank you very much, RW!
kalee20 is offline  
Old 19th Jun 2018, 2:25 pm   #15
dave cox
Nonode
 
dave cox's Avatar
 
Join Date: Jan 2009
Location: Bristol, UK.
Posts: 2,059
Default Re: Heavy maths? Sample rates and pitch shifting

As kalee20 said, a high order polynomial will very likely go wild in between the fitted points! I saw some amusing results from a graph plotter once that would demonstrate this very well

For N output samples, in regular sample interval, you will need to find - at most - N polynomials. If the number of input samples, in irregular sample interval, is less than N you might be computing multiple output samples from each polynomial. I suggest 4 as it might give better results that 2 ( the linear case, is that just a ‘nomial ). If there was post filtering, there may be no advantage in doing anymore work than the linear approach.

dc
dave cox is offline  
Old 19th Jun 2018, 4:36 pm   #16
woodchips
Octode
 
Join Date: May 2010
Location: Grantham, Lincolnshire, UK.
Posts: 1,172
Default Re: Heavy maths? Sample rates and pitch shifting

Assuming this is speech.

Reading out the samples faster or slower than the rate they were stored at will change the pitch, presumably the problem is that the original length of time of the recording will change, less or more? Not certain why this is a problem?

A similar situation I had was changing the speed of a recording playback but keeping the pitch constant, don't get Pinky & Perky. If the pitch change is ok then what is the problem? What is changing that you don't want to change? Seems to me that it is either sample rate or pitch?

This is work done 35 years ago, struggle to remember exactly how it worked.
woodchips is offline  
Old 19th Jun 2018, 4:59 pm   #17
Radio Wrangler
Moderator
 
Radio Wrangler's Avatar
 
Join Date: Mar 2012
Location: Fife, Scotland, UK.
Posts: 22,801
Default Re: Heavy maths? Sample rates and pitch shifting

And, of course, converting baseband speech to SSB at an elevated frequency and then converting it not quite back to the original frequency is a way of pitch-shifting that doesn't compress or stretch the rate of talking or length of a recording BUT (big but!) it is an arithmetic shift of frequency and not a geometric scaling. The whole spectrum is slid not scaled.

So harmonics in the speech (music is hilarious) cease to be on integer multiples of their fundamentals. You can't put your finger on it, but something sounds wrong.

This is how operators tune in to SSB transmissions and fine-tweak the tuning.

So we got back to the original subject in the end

DAvid

Oh, by the way, while the audio is at an elevated frequency in SSB form, it can be clipped and the harmonics formed by clipping are at multiples of the elevated frequency and far out of band. The SSB can be brought back down and speech is far punchier for comms radio transmission, there will still be some intermod added, but you get clipping without most of the distortion.
__________________
Can't afford the volcanic island yet, but the plans for my monorail and the goons' uniforms are done
Radio Wrangler is offline  
Old 19th Jun 2018, 5:25 pm   #18
GMB
Dekatron
 
GMB's Avatar
 
Join Date: Aug 2003
Location: near Reading (and sometimes Torquay)
Posts: 3,086
Default Re: Heavy maths? Sample rates and pitch shifting

Exactly. I do not think SSB style frequency shifting will be any use.

The question remains as to the intent.

If you insert extra samples, which I think is best done by resampling the whole thing rather than putting in occasional extra samples, the effect will be like a record slowing down. Pitch will be reduced and time stretched.

If you want it to just sound the same, but a lower pitch, then I think you have to go the Fourier route unless someone knows a clever digital filter that has the same effect.
GMB is offline  
Old 19th Jun 2018, 5:37 pm   #19
PJL
Dekatron
 
Join Date: Sep 2005
Location: Seaford, East Sussex, UK.
Posts: 5,997
Default Re: Heavy maths? Sample rates and pitch shifting

Of course it could be audio and this is to remove wow&flutter?

In this case linear interpolation would be better as the problem was the samples were not taken at fixed intervals in the original recording.
PJL is offline  
Old 19th Jun 2018, 6:19 pm   #20
julie_m
Dekatron
 
Join Date: May 2008
Location: Derby, UK.
Posts: 7,735
Default Re: Heavy maths? Sample rates and pitch shifting

The actual intent is correcting a tape recorded on a spool-drive machine (therefore CAV) played back on a capstan-drive machine (therefore CLV). With each full turn of the spool, the radius onto which the tape is being wound increases by one tape thickness; thus, each metre of tape (462100 input samples) represents fewer seconds as you go along.

Correcting wow and flutter on CLV recordings would be nice. It did occur to me; and I probably could do it, if I could get a good "fix" on any mains hum present in the recording.
__________________
If I have seen further than others, it is because I was standing on a pile of failed experiments.
julie_m is offline  
Closed Thread

Thread Tools



All times are GMT +1. The time now is 6:00 am.


All information and advice on this forum is subject to the WARNING AND DISCLAIMER located at https://www.vintage-radio.net/rules.html.
Failure to heed this warning may result in death or serious injury to yourself and/or others.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright ©2002 - 2023, Paul Stenning.