Showing posts with label ADC. Show all posts
Showing posts with label ADC. Show all posts

Friday, August 20, 2021

Analogish bit crusher

 I just came across this great post about an analogish bit crusher and sample rate reducer from Juanito Moore and Kristian BlĂ„sol in the Synth-DIY group on facebook:


When I made my mcu-based bit crusher I was thinking about doing something similar but couldn't think of how to do the actual bit crushing - this solves that :)

But that got me thinking - could we do away with the ADC too?

Turns out we can of course. By making a flash ADC using comparators for example:


Instead of the 8-to-3 line encoder we could put D flip-flops and clock them, to reduce sample rate. Maybe we could even find a way to re-use the flip-flop as a switch, to combine it with the bit crush-comparator outputs? D flip flops with preset and clear seem able to do this as long as preset/clear are level controlled, not edge.

We can also use the 40174 hex D-storage, which stores 6 bits of info (but does not have a preset though)

If we want to go all-analog we could instead use sample and hold buffers for the bits, though we may not be able to have a combination of both very high and low sample rates as the leakage during hold vs charge time during sample may be incompatible. This could be improved by using a second set of comparators on the output of the S&H so that a voltage drop won't be visible on the DAC side of things, but now things start getting a bit complicated :-D


All in all not a terribly practical project, but fun none the less.

Tuesday, October 6, 2020

Nyquist and reconstruction filters in practice!

I assume you've heard about the Nyquist sampling theorem? It basically says that you have to sample a signal at twice the sample rate of the highest frequency you want to represent, that's why CDs have a sample rate of 44kHz - to be able to reproduce signals around 20kHz.

You may even have heard that doing so, you are not only able to reproduce an approximation of the original signal - you can reproduce an exact copy.

But have you actually seen this happen in practice? Let me show you! 


Sampling

A/D and D/A conversion is fairly simple in its basic form. Use an ADC and check the signal amplitude/level at set intervals. DAC is the reverse, set the amplitude of the output to whatever you got from the ADC (There is more to it than that for high quality signal reproduction, but for the sake of the discussion, and for the level of accuracy I need this suffices). 

Try this at home, looking at the output on an Oscilloscope and you'll quickly realise that the output looks nothing like the input. It has jagged edges, you can clearly see the height and length of the individual samples as output from the DAC. 

This is because there is one (two) more very important element(s) needed. To prevent aliasing - a higher frequency signal (outside of the audible frequency range) masquerading as a lower frequency one, we need to use a low pass filter before the ADC to remove frequencies that cannot properly be captured, e.g. frequencies above 1/2 of the sampling frequency.

When playing back the signal we need to do the same - as the DAC outputs a stepped signal it will generate lots of higher frequencies as well. By using a filter with the same cutoff - this time called a reconstruction filter - those are removed (once again, this is more complicated in practice for a high quality signal, but let's forget about that for a minute).


Ok, so that all sounds great on paper. Output a stepped signal and apply some magic, and you should get the original signal. But it can't be that easy, right?


Let's look at some photos:


DAC output (top) vs output after reconstruction filter
DAC output (top) vs output after reconstruction filter

Here we see the output of a sampled sine wave (top) vs the output after the 20kHz reconstruction filter. It looks amazing! It works for other waveshapes as well of course, though if we were to zoom in on the edges of the triangle wave we would see that it's slightly rounded - this is because the frequencies required to reproduce it faithfully have been removed by the two filters. This is of course not a big deal as we wouldn't hear them anyway. 

Sampled and reconstructed triangle wave, tips are rounded by filtering.


The effect is even more pronounced with square waves which get a clear oscillation at the edges



Increasing the frequency of the input shows that the DAC output no longer resembles the input as closely - it has very few samples per cycle of the sine wave:




Now things start getting strange. Increasing the frequency even further "distorts" the DAC output even more. Remember, Nyquist says that it should be possible to reproduce a sine wave with only TWO samples per cycle. It sounds incredibly strange, but it actually works! Here is a 15kHz signal, filtered at 20kHz. The DAC output looks nothing like the sine wave it is supposed to represent:


Seeing this for the first time blew my mind. How is it even possible?! Well, read up on the theory behind it in Steven W. Smiths awesome book "The Scientist and Engineer's Guide to Digital Signal Processing" (available for free as separate pdfs from his web page) if you want the theory.


Effects of filtering


Now, there are some effects of filtering still present. First, we get some phase distortion, which increases as we approach the cutoff frequency of the filters. Looking at the input and output sines we can see that they don't line up properly:


As far as I know, this is not audible on its own. It WILL however be audible if the signal is mixed back with the original signal (which incidentally is how phasers are made). (PS: The photo is slightly misleading. Some of the apparent phase shift is only a delay between the input and output due to the time it takes for the DSP chain to process the signal. But the phase shift definitely increases as we approach the filter cutoff)

The second effect is that the amplitude (volume) of the output will decrease as we approach the filter cutoff. This is nothing special to this circuit, it's just how filters work - they do not have an absolute cutoff point, instead they gradually attenuate more and more. Having higher order filters with more stages will help - I'm using six poles in mine. High quality reconstruction filters however, do this differently. As we know how the filter affects the amplitude, we can do the reverse in advance - increase the volume of those frequencies. This cancels out the effect of the filter, keeping the volume constant for much longer. I have not bothered with this in my circuit as we're talking about fairly high frequencies anyway.

Thursday, July 23, 2020

Quick thought on bitcrusher

The bitcrusher works in two ways - it reduces sample rate and it reduces the bit resolution.

A good quality audio DAC circuit needs a reconstruction filter after the DAC to remove unwanted high frequencies ( >20kHz). When sample rate is reduced, this filter's cutoff must also be reduced to approx twice the sampling rate to keep the quality.

BUT - in a bitcrusher, don't we actually WANT the coarseness caused by not filtering the signal perfectly? I assume so. This has to be tested.

I will try building a bitcrusher with a simple ADC->MCU->DAC->static LPF and see. 

https://en.wikipedia.org/wiki/Bitcrusher

Monday, May 25, 2020

TIL: The little phatty has an analog signal path from pots to synth circuts

I read a reddit thread this morning about a guy that is doing a synth controller front panel. He had the novel idea of switching between using the pots directly to feed an analog voltage to the synth circuit (filter cutoff cv etc) and using voltages from a DAC to do the same. By simultaneously reading the voltage from the pot for patch storage, it would then be possible to load a patch and have a dac generate the required voltage to control the synth, but switch to direct-from-pot cv when the user turns the pot to prevent stepping.

The original poster was immediately beaten down by the knowitalls of Reddit, saying that this was overly complicated and that noone would do it like this.

He countered by claiming that he had looked at the circuit board of a Moog synth and seen analog multiplexers close to each pot, and rationalised that these did what he was thinking about. He also claimed that a video told him that this was the way Moog did it.

Another poster then proceeded to post the schematics for the Slim Phatty, explaining how it was clear to see that the circuits were indeed digitally controlled.

I had a closer look at the schematics and discovered that the original poster was indeed right (though possibly not about exactly how it was done) and the one posting the schematics wrong - the Slim Phatty (and by extension Little Phatty) DO actually have the ability to switch between direct control and digital control. Have a look at this:


The filter pot. Its value is buffered by an opamp (U29), which is then both tapped directly (FILTER_CV) and sent to an ADC through AINO.2 for digital processing/storage.

Digital control: CV from the DAC is buffered by a sample and hold circuit and output as FILT_CUTOFF_BUF

A switch then selects between the CV tapped directly from the pot (FILTER_CV) and the digitally controlled voltage (FILT_CUTOFF_BUF). Notice how FILTER_CV is sent to multiple outputs, depending on what mode is currently selected for the pot.

The selected voltage is sent to the board connector as FILT_CUTOFF...

...and received on the voice bort as FILT_CUTOFF (pin 33)

There, it is mixed with CVs from other sources (modulation etc) into the filter cutoff CV FILT_CONTROL

Finally, FILT CONTROL controls the cutoff frequency of the ladder filter. Thus there is a completely analog signal path from pot to filter.

When I went back to post my findings on Reddit, the original poster had already done so. He pointed out that this is called RAC (tm) in the Slim Phatty manual - "Real Analog Control".

The discussion went on to saying that this must be a marketing trick more than having real value, and funnily enough saying that it had do be something only the phattys use because "the memorymoog and prophet 5 does not do it". I call bullshit on this.It may be true that it is only found on the phattys, but comparing it to 25 years older synths is no way of proving it...