Arduino Forum

Using Arduino => Audio => Topic started by: zkhan on Feb 22, 2019, 08:04 am

Title: Real Time Audio Processing
Post by: zkhan on Feb 22, 2019, 08:04 am
Hello,

I had a question regarding real time audio processing. Im trying to create a project that takes in a 3.5 mm jack/ and or mic input to A0 coverts it through ADC and outputs it on the audrino pins of the DAC. I have already created a preamp that takes in a mic into A0 as well as a 3.5 jack.

Furthermore Ive created a R2R resistor ladder with a low pass filter, power amplifier, and output. I know the R2R ladder can be low quality. Ive inserted an image of my dac circuit below. As well can be seen in this link https://www.instructables.com/id/Arduino-Audio-Output/


However my question is there a way I can code to take the audio and output it to the R2R resistor ladder I am fairly new to this and any help is greatly appreciated. Thank you! (preferable register programming help rather than libraries)
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Feb 22, 2019, 11:03 am
Unless you use resistors that are matched to within 0.2% of each other than that circuit will cause all sorts of distortions in your already poor audio signal.
It is poor because you have a limited sample rate.
You will need to use the direct register addressing to write to them and you will need more than one access because you do not have a clear run of 8 bits free in any register used with an Arduino. This will cause additional glitches in your sample.

But your big problem is lack of memory for the processing code. Unless you can add some external memory what you could do will be limited.
Title: Re: Real Time Audio Processing
Post by: PieterP on Feb 22, 2019, 01:16 pm
The AVR microcontrollers used in Arduinos are not suited for audio processing.
They were simply not designed with audio in mind: they lack memory, processing power, ADC resolution and speed, and a DAC.

Use a microcontroller with more memory, I²S support, DMA, etc.
Take a look at ARM microcontrollers like STM32 if you have some experience, or use a Teensy if you want a more beginner-friendly platform.
You could probably also use an ESP32.

If you're interested in learning low-level DSP, the CMSIS-DSP library (https://developer.arm.com/embedded/cmsis) might be useful.

If you don't care about DSP at all, and you just want to get your audio project working, you can use the PJRC Audio library (https://www.pjrc.com/teensy/td_libs_Audio.html).

freedsp.cc (http://freedsp.cc) is also worth a look.

Pieter
Title: Re: Real Time Audio Processing
Post by: MarkT on Feb 22, 2019, 02:21 pm
You can't parallel opamps like in your circuit, if you want more current than one opamp can supply there is
a way to do it: https://e2e.ti.com/blogs_/archives/b/thesignal/archive/2013/03/26/paralleling-op-amps-is-it-possible (https://e2e.ti.com/blogs_/archives/b/thesignal/archive/2013/03/26/paralleling-op-amps-is-it-possible)

The resistor ladder needs most precision in the resistors closer to the output end, note.

With tight coding you can do this audio processing to some extent, you'll need to up the ADC clock speed a bit
I think, and restrict the processing to cheap operations.  An external SPI ADC can be a lot quicker to read
(compared to waiting for the on-chip ADC), freeing lots of processor cycles.

You can even do noise-shaping to increase the effective number of bits on the output for lower frequencies,
but you need a high sampling rate for this to be effective.
Title: Re: Real Time Audio Processing
Post by: ard_newbie on Feb 22, 2019, 03:37 pm
This I2S library  (https://github.com/delsauce/ArduinoDueHiFi)allows the Arduino DUE to interface with a large number of audio codecs and enables higher quality audio I/O than what is available with the standard on-chip ADC and DACs.


To make use of the on-chip ADC and DACs plus a real time digital filtering, there is this  (http://www.f-legrand.fr/scidoc/docmml/sciphys/arduinodue/filtrage/filtrage.html)tutorial (google translate is your best friend).



Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Feb 22, 2019, 05:11 pm
The resistor ladder needs most precision in the resistors closer to the output end, note.
No, think about it. To achieve monotonicity all resistors have to be of the same precision.
Title: Re: Real Time Audio Processing
Post by: Lucario448 on Feb 22, 2019, 06:11 pm
However my question is there a way I can code to take the audio and output it to the R2R resistor ladder [...] (preferable register programming help rather than libraries)
Not sure why you want to an audio passthrough with an Arduino (besides a proof of concept maybe), but it's not that difficult:

Code: [Select]
void setup() {
  DDRD = 255; // The whole port as an output
  // ADC settings that speed it up and I don't remember the register name.
  TIMSK0 = 0; // Improves "sampling rate" a little more, but it's optional (since this disables delay and time functions)
}

void loop() {
  PORTD = analogRead(A0) >> 2;
}

Since this is a simple passthrough, precise timing of the sampling is not that important. It may go as fast as it can.
Title: Re: Real Time Audio Processing
Post by: MarkT on Feb 22, 2019, 06:27 pm
No, think about it. To achieve monotonicity all resistors have to be of the same precision.
I did think about it, the resistor precisions are weighted by bit position, a 10% resistor for the LSB is
a smaller deviation than a 0.1% resistor in the MSB for an 8 bit ladder.
Title: Re: Real Time Audio Processing
Post by: zkhan on Feb 22, 2019, 07:37 pm
Thank you for all you helpful responses.  Any tutorial or simplest way I can accomplish this task. Tutorial maybe. Thank you.
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Feb 22, 2019, 07:42 pm
I did think about it, the resistor precisions are weighted by bit position, a 10% resistor for the LSB is
a smaller deviation than a 0.1% resistor in the MSB for an 8 bit ladder.
No the contribution of the most significant bit must be as accurate as the least. Because 0111 1111 must be lower than 1000 0000
Title: Re: Real Time Audio Processing
Post by: DVDdoug on Feb 22, 2019, 08:00 pm
Quote
Thank you for all you helpful responses.  Any tutorial or simplest way I can accomplish this task. Tutorial maybe. Thank you.
What is "this task"?     You didn't say what quality/resolution you want and you didn't say what kind of audio processing you want to do.   

I think some people have made simple-limited guitar pedal effects...   But, the Arduino is not going to do anything high-fidelity or highly-complex.   (And DSP tends to be mathematically complex unless you're something simple like volume manipulation.)

You might want the MiniDSP (https://www.parts-express.com/minidsp-2x4-hd-usb-dac-digital-signal-processor--230-324) or the Dayton DSP (https://www.parts-express.com/dayton-audio-dsp-408-4x8-dsp-digital-signal-processor-for-home-and-car-audio--230-500)  ?
Title: Re: Real Time Audio Processing
Post by: PieterP on Feb 22, 2019, 08:01 pm
Thank you for all you helpful responses.  Any tutorial or simplest way I can accomplish this task. Tutorial maybe. Thank you.
The simplest way is to put aside the Arduino and connect the output of the microphone preamp to the input of the speaker amplifier.

What do you really want to do?

I already posted a link to the Teensy Audio library, it's not going to get easier than that.
Title: Re: Real Time Audio Processing
Post by: zkhan on Feb 22, 2019, 08:44 pm
Sorry for bad english. Like there is no other way to create a program with the implementation above. (R2R ladder with A0 preamp).

For example using the ADC of the audrino and a DAC Tutorial to output an analog signal from A0
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Feb 22, 2019, 09:11 pm
OK forget the D/A and try this:-
http://interface.khm.de/index.php/labor/experimente/arduino-realtime-audio-processing/ (http://interface.khm.de/index.php/labor/experimente/arduino-realtime-audio-processing/)
Title: Re: Real Time Audio Processing
Post by: zkhan on Feb 22, 2019, 11:32 pm
I had a question, so I was able to implement the code below with the specification i said above. However the static noise is very loud. I already have a low pass filter as stated up in my specification. Anything i can do to reduce the static.



Code: [Select]

void setup() {
  //
  cli();
 
  //
  TCCR2A = 0x00;
  TCCR2B = 0x00;
  TCNT2 = 0x00;
  OCR2A = 249;
  TCCR2A |= (1 << WGM21);
  TCCR2B |= (1 << CS21);
  TIMSK2 |= (1 << OCIE2A);
 
  //
  sei();

}

ISR(TIMER2_COMPA_vect) {
  //
  analogWrite(10, map(analogRead(0), 0, 1023, 0, 255);
}

void loop() {
 
}
Title: Re: Real Time Audio Processing
Post by: MarkT on Feb 23, 2019, 05:37 am
By static do you mean aliasing/quantization noise?  Do you have a sample of this to listen to?

What frequency is timer2 running at?
Title: Re: Real Time Audio Processing
Post by: Lucario448 on Feb 23, 2019, 08:48 pm
What frequency is timer2 running at?
Assuming the typical 16 MHz clock frequency, then the interrupt is firing up at a rate of 64.25 KHz (every 15.56 microseconds).
Although with the ADC's default prescaler, effective sampling rate is more like 10 KHz or even less. Successive approximation ADCs aren't known for fast conversions; so in order to achieve high sampling rates, their clock shouldn't be too slow.





PD: the map() function is rather slow also. The CPU is 8 bits "wide" (against the 32-bit variables it uses inside), and its ALU lacks integer division support (such operation is actually done in software, and the function needs it).
Since you're scaling two power-of-two-wide ranges, the fastest way to accomplish the same result, is to move around some bits. Narrowing 10-bit values into an 8-bit input is possible simply by discarding two least significant bits (aka right shift 2 bits). In the end, it's something like this:
Code: [Select]
analogWrite(10, analogRead(A0) >> 2);

Furthermore, you may even want to change the duty cycle quicker. Instead of analogWrite(), you can set a single register to achieve the same effect.
Pin 10 belongs to the channel B of the timer1, so the correct register is called OCR1BL (ends with 'L' because that's a 16-bit timer and you only need the LSB to set the duty cycle). Improving that line even more:
Code: [Select]
OCR1BL = analogRead(A0) >> 2;

I'm suggesting all this because 15 microseconds is a quite tight time budget for an AVR microcontroller, thus having to do everything as "low level" as possible.




PD 2: I've just realized another problem: you're using a timer1 output, which defaults to a prescaler of 64 and runs in "phase-correct" mode; resulting in a carrier frequency of just 490 Hz. I don't think you'll manage to modulate like that any meaningful audio signal, do you?
Title: Re: Real Time Audio Processing
Post by: MarkT on Feb 23, 2019, 10:44 pm
SAR converters can be fast, but not so much the one the ATmega chip...

(I know this because I'm just starting to play with an ADS8885, 400kSPS 18 bit SAR ADC chip with 1.2µs
settling time to full precision...)
Title: Re: Real Time Audio Processing
Post by: zkhan on Feb 24, 2019, 03:41 am

Quote
I don't think you'll manage to modulate like that any meaningful audio signal, do you?
I can actually have really bad quality signal come out of it. You can make out the sounds and words a little bit. And thank you for the insight. Furthermore, is there a way I can increase the quality of the sound in a low level aspect? Thank you!
Title: Re: Real Time Audio Processing
Post by: JarkkoL on Feb 24, 2019, 05:51 am
8bit R2R DAC can generate decent sound and you should definitely make out the sounds and words. Here's a reference of 8bit DAC playing audio on Arduino Uno and this doesn't even have any filter, just resistors: https://youtu.be/VHn9NBTj-ME (https://youtu.be/VHn9NBTj-ME)

You need to eliminate the issue in the components you have. E.g. is your DAC actually producing proper signal. If you don't have oscilloscope, make little program to output sine wave for example and listen do you get proper output. Is the analog input working? With silent sound what's the value. How does the range of values change as the sound gets louder (record min & max). Is there clipping when you direct input to output?
Title: Re: Real Time Audio Processing
Post by: zkhan on Feb 24, 2019, 06:22 am
The file you sent have very complex code probably not suitable for me. Moreover, I wanted to know if i can optimize my code above to get better quality. Thank you for the insight though.


Quote
8bit R2R DAC can generate decent sound and you should definitely make out the sounds and words.
Title: Re: Real Time Audio Processing
Post by: PieterP on Feb 24, 2019, 10:03 am
While many great points have been raised, and even though it's interesting as a proof of concept, I think it's just a waste of time.

An Arduino UNO is not suited for this task, especially if the OP just wants his/her project working and has limited experience with the topic.

@OP, you still haven't told us what you really want to do. Having the Arduino read an input voltage and output the same voltage is not really useful, and only causes signal degradation.
What kind of "real time audio processing" do you want to do?

Edit: it seems like OP has opened a new thread on the same topic: http://forum.arduino.cc/index.php?topic=599651.0 (http://forum.arduino.cc/index.php?topic=599651.0)
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Feb 24, 2019, 10:29 am
Reported other thread.

Do not cross post.
Title: Re: Real Time Audio Processing
Post by: MarkT on Feb 24, 2019, 08:36 pm
No the contribution of the most significant bit must be as accurate as the least. Because 0111 1111 must be lower than 1000 0000
Nope, you are assuming all the currents from each bit are
(a) the same (they are not), and
(b) all contribute equally to the output node (they do not).

Basically in an R-2R ladder each bit has half the influence of the previous one, so tolerances for resistors
and voltage references get less strict along the string.

When outputing 10000000, the node on the string next to the LSB is at about 20mV, so changes in
the value of the resistor there have a tiny effect compared to bit 7 whose resistor sees about 2.5V

For instance if R = 5k and 2R = 10k, bit 0 is sinking 2µA, bit 6 sinking ~125µA, bit 7 sourcing ~250µA

Switching to 01111111 reverses the sense of all the voltages and currents, but bit 0 only changes from
sinking to sourcing 2µA, a 4µA change
Title: Re: Real Time Audio Processing
Post by: zkhan on Feb 25, 2019, 12:01 am
Quote
@OP, you still haven't told us what you really want to do
Thank you for all the information. Sorry for cross Posting. Moreover, I wanted to create/manipulate a timer function that can allow better sound quality, because Ive seen individuals creating really good quality sound with a R2R resister ladder.

Sorry again for the many question, however reading more forums i found that you can "If you need 16 bits, you can use 2 R2R ladders and tie the output of the low byte R2R to the input of the high byte R2R and get a clean 16 bit analog output". Do y'all think it would increase the audio signal, because currently I'm using a 1 R2R resistor ladder? But the built in ADC is 8 bit, so will that aswell cause problems?


Thank you
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Feb 25, 2019, 09:54 am
The built in A/D is 10 bits.
Making a home built 16 bit D/A using two ladders is an illusion and put on the web by people who frankly don't know what they are doing and don't know how to test it correctly. The accuracy of the resistor ratios needed is just not attainable from fixed resistors.

How can a timer function create better audio?
Pleas tell us what you actually want to do.
Title: Re: Real Time Audio Processing
Post by: MarkT on Feb 25, 2019, 11:45 am
You can synthesize upto 16 bit accuracy from 2 8-bit PWM signals and two accurate resistors in the ratio 1:256,
which hasn't been mentioned on this thread yet.
Title: Re: Real Time Audio Processing
Post by: PieterP on Feb 25, 2019, 12:38 pm
You can synthesize upto 16 bit accuracy from 2 8-bit PWM signals and two accurate resistors in the ratio 1:256,
which hasn't been mentioned on this thread yet.

Would it be enough for the PWM signals to have the same frequency and phase offset? Edge-aligned or phase-correct PWM?
Or should both signals be LPF'ed first, and then combined?

I think it's probably easier to just use Timer1 for 16-bit PWM.

However, a 16-bit DAC isn't of much use if your ADC is only 10 bits or even 8 bits at higher speeds, but I don't think OP realizes this ...
Title: Re: Real Time Audio Processing
Post by: MarkT on Feb 25, 2019, 12:57 pm
But you can also combine two 5 bit PWMs to get 10 bit, and 5 bit PWM can run a lot faster for easier
low-pass filtering on the output.
Title: Re: Real Time Audio Processing
Post by: PieterP on Feb 26, 2019, 12:29 pm
But you can also combine two 5 bit PWMs to get 10 bit, and 5 bit PWM can run a lot faster for easier
low-pass filtering on the output.
True, I didn't think about that!
Title: Re: Real Time Audio Processing
Post by: MarkT on Feb 26, 2019, 03:57 pm
Would it be enough for the PWM signals to have the same frequency and phase offset? Edge-aligned or phase-correct PWM?

You should always use phase-correct PWM for audio, it has a lot less harmonic distortion.
The frequency and phase relationship is irrelevant, as the duty cycle carries the signal, but you'd
need to make sure the frequency difference didn't fall in the audio band and alias all over it - theres
no reason to do anything but use the same frequency
Quote
Or should both signals be LPF'ed first, and then combined?
Why would that make any difference?  They are both linear operations.  Save components and use one filter.

[ PWM and distortion: http://www.openmusiclabs.com/learning/digital/pwm-dac/pwm-distortion-analysis/index.html ]
Title: Re: Real Time Audio Processing
Post by: Lucario448 on Feb 26, 2019, 06:51 pm
I think it's probably easier to just use Timer1 for 16-bit PWM.
Easier said than done.

At a given oscillator speed, you trade off resolution for frequency (at a power of 2).
Furthermore, timer1 makes use of all its 16 bits only in normal or CTC mode (for timed interrupts only). In PWM mode, from 8 up to 10 bits are used, being that last one the maximum possible resolution for hardware-generated PWM (at least for AVRs though).

And it makes sense, if an MCU that runs at 16 MHz could produce 16-bit PWM signals, their maximum frequency would be 16M / 65536 = 244.14 Hz. Well... even worse than timer1 and 2 at their default settings.
Faster but "cheap" alternatives (such as the "Blue Pill") aren't that much better either; somewhere close to 80 MHz would yield around 1 KHz.

To get a (single) 16-bit PWM signal that would actually produce decent-quality audio, the system would have to run at 5-6.5 GHz (good luck finding a PC CPU that fast but without overclocking).



As you can see, hi-res hi-rate PWM DACs are practically impossible, unless you combine two low-res outputs (which is the only feasible way to achieve high-quality audio merely by PWM). Resistor-ladder DACs don't have this problem since their trade-offs work differently (resolution for simplicity/cheapness, the frequency is up to how often the system is willing to toggle its digital outputs).
Title: Re: Real Time Audio Processing
Post by: MarkT on Feb 27, 2019, 11:08 am
As you can see, hi-res hi-rate PWM DACs are practically impossible.
But possible, which is what multibit sigma-delta DACs do.  Its all in the noise shaping. Take
raw 5 bit PWM say, at about 1MHz, throw in multiple-order noise shaping can get you to 20 or more
bits of precision at lower audio frequencies. Think of it as an advanced version of dithering/averaging.
The quantization noise is pushed up in frequency where it can easily be filtered out.  Its a trade off
of clock speed for precision.  32MHz clocked PWM generates 20kHz of clean bandwidth.  The picture
in the attic is all the noise above 20kHz.
Title: Re: Real Time Audio Processing
Post by: zkhan on Feb 28, 2019, 12:13 am
Thank you once again with all the information. I had a quick question, so the code i written in the page 1

Code: [Select]

void setup() {
  //
  cli();
 
  //
  TCCR2A = 0x00;
  TCCR2B = 0x00;
  TCNT2 = 0x00;
  OCR2A = 249;
  TCCR2A |= (1 << WGM21);
  TCCR2B |= (1 << CS21);
  TIMSK2 |= (1 << OCIE2A);
 
  //
  sei();

}

ISR(TIMER2_COMPA_vect) {
  //
  analogWrite(10, map(analogRead(0), 0, 1023, 0, 255);
}

void loop() {
 
}


-This code allowed fairly some music to play, unfortunately applying that now, we do not get the same result. Im a mic amplifier which thus goes to A0 an a 8 bit R2R resistor ladder. Reading some forums i see that the same voltage from the same audrino sometimes causes problems could that be the case. I tried giving the mic a separate voltage source and the ground connected to the Dac of the audrino. May someone know what the problem may be
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Feb 28, 2019, 12:16 am
Quote
Reading some forums i see that the same voltage from the same audrino sometimes causes problems could that be the case.
That is very vague. Can you post a link to those forum threads you talk of.
Title: Re: Real Time Audio Processing
Post by: zkhan on Feb 28, 2019, 12:31 am
Unfortunately, I dont remember exactly. I apologize. Also because I am trying to finish in about 2 days with some listenable audio that can you can hear from a 8 bit R2R ladder. Is there a way tall know of?
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Feb 28, 2019, 12:59 am
Quote
This code allowed fairly some music to play, unfortunately applying that now, we do not get the same result.
Are you saying that this code used to work and now it does not?

If so then your circuit is not what it was. It has changed or you have loose intermittent connections.
Title: Re: Real Time Audio Processing
Post by: zkhan on Feb 28, 2019, 07:57 am
I have one more question. So how could i increase the sampling rate to 8Hz and send the data to the R2R Dac -- the code below.

I know that atmeag328 rums at 16MHZ so if we use a prescaler of 128 it would equal to 125KHZ and it takes 13 cycles therefore 125/13 = 9615 Hz. With nyquist theorem that would be about half the quality i could reproduce.

How can i change my code to get 8KHZ sampling rate and as well as sending that data to the R2R Dac. I believe i can do PORTD = ADCH;

However, there are a few details. I will want to left justify the ADC read value, and I will to use only the high byte.
Then i will want to connect the R-2R ladder to PORTD in the right order. The most significant bit will be output on pin PD7 and the least bit will be on PD0.

I need help with Setting the sample rate and the other configuration details needed for this task.  Thank you Again for all the help

Code: [Select]

void setup() {
  //
  cli();
 
  //
  TCCR2A = 0x00;
  TCCR2B = 0x00;
  TCNT2 = 0x00;
  OCR2A = 249;
  TCCR2A |= (1 << WGM21);
  TCCR2B |= (1 << CS21);
  TIMSK2 |= (1 << OCIE2A);
 
  //
  sei();

}

ISR(TIMER2_COMPA_vect) {
  //
  analogWrite(10, map(analogRead(0), 0, 1023, 0, 255);
}

void loop() {
 
}
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Feb 28, 2019, 10:37 am
Quote
I will want to left justify the ADC read value, and I will to use only the high byte.
Shift to the right by two places.
Code: [Select]
aIn = analogueRead(pin);
justified = aIn >> 2;


Quote
I need help with Setting the sample rate
From the looks of that code you do not have a framework that anybody can help you with yet, unless help means doing it for you.
Quote
This code allowed fairly some music to play
No it didn't.

The prescaler defaults to 128 so the sample rate is already at about 10K.

Hang an ISR off the ADC converter compleate interrupt. Have it read the ADC results registers and do the shift then output to port D.
See https://www.arduino.cc/en/Reference/PortManipulation (https://www.arduino.cc/en/Reference/PortManipulation) for examples of how to write to ports.
Why does it have to be exactly 8KHz?
Title: Re: Real Time Audio Processing
Post by: zkhan on Feb 28, 2019, 03:48 pm
Like this ? Thank you

Code: [Select]
void setup()
{
  Serial.begin(115200);
 
  DDRD = 0xFF;
  ADCSRA = 0;             // clear ADCSRA register
  ADCSRB = 0;             // clear ADCSRB register
  ADMUX |= (0 & 0x07);    // set A0 analog input pin
  ADMUX |= (1 << REFS0);  // set reference voltage
  ADMUX |= (1 << ADLAR);  // left align ADC value to 8 bits from ADCH register
 
  ADCSRA |= (1 << ADPS2); // 16 prescaler for 76.9 KHz
  ADCSRA |= (1 << ADATE); // enable auto trigger
  ADCSRA |= (1 << ADIE);  // enable interrupts when measurement complete
  ADCSRA |= (1 << ADEN);  // enable ADC
  ADCSRA |= (1 << ADSC);  // start ADC measurements
}

ISR(ADC_vect)
{
  PORTD = ADCH; // output ot Port D
}

void loop(){
 
}
Title: Re: Real Time Audio Processing
Post by: Lucario448 on Feb 28, 2019, 05:36 pm
Code: [Select]
ISR(ADC_vect)
{
  PORTD = ADCH; // output ot Port D
}

You'll barely hear anything like this; only the two most significant bits are retrieved. The right way to narrow down ADC readings is this:

Code: [Select]
ISR(ADC_vect)
{
  PORTD = (ADCL >> 2) | (ADCH << 6);
}

Also, don't worry about bit order; the MSB of the register is indeed wired to PD7 (pin 7); as well as the LSB to PD0 (pin 0 / hardware USART RX).




PD: by the way, if you started with the idea of a R-2R DAC, then why you showed us an example of PWM DAC at some point?
Title: Re: Real Time Audio Processing
Post by: Lucario448 on Feb 28, 2019, 05:45 pm
I've forgot to point out another mistake.

If you call Serial.begin(), you set up the USART port; and if you set up the USART port, it will override PD0 and PD1. This will make those pins unresponsive to any write attempt to PORTD and DDRD.
Title: Re: Real Time Audio Processing
Post by: zkhan on Feb 28, 2019, 06:20 pm
Thank you so much. However the quality is still pretty tough.any suggestions in increasing quality. Can I do a 5 bit dac two of them to get a 10 bit dac as previously stated in this forum? Or what other methods can I do to improve the quality? I know it's a bit which can only get good sound quality but still wanted to know. Thank you for helping.
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Feb 28, 2019, 06:35 pm
Quote
However the quality is still pretty tough.any suggestions in increasing quality.
Make sure that the audio input is a 5V peak to peak signal, this makes the most of every bit you have.

This is the sort of quality you can expect from 8 bits using a PWM modulated output.
https://youtu.be/WShVFcrFpwU (https://youtu.be/WShVFcrFpwU) and the full range of the 8 bit D/A it produces.
Title: Re: Real Time Audio Processing
Post by: zkhan on Feb 28, 2019, 07:32 pm
Make sure that the audio input is a 5V peak to peak signal, this makes the most of every bit you have.

How can I make sure? Thank you again
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Feb 28, 2019, 10:53 pm
Quote
How can I make sure?
The only real way is to use an AC millivoltmeter or best of all an oscilloscope. It is a bit pointless trying to do anything in audio without a scope.

However, you could write some code that inputs the data into a small buffer, say 512 bytes, and then prints out the numbers, not to the serial monitor but to the serial plotter so you get a graph. Then you can see how big your input waveform is.

Oh and by the way:-
Quote
Can I do a 5 bit dac two of them to get a 10 bit dac
You can but it will be no better than just having a 10 bit R/2R ladder, and like I said you can't buy resistors of good enough tolerance even to make an 8 bit ladder let alone a 10 bit one. I don't think you understand this point.
Title: Re: Real Time Audio Processing
Post by: zkhan on Mar 01, 2019, 01:34 am
Thank you for the insight I will do that. Moreover I want to add a 3.5 m jack and leave the code alone .
I was able to find this however im not too certain was the values of the resistors and cap would be?

I connected my 3.5 jack which heres the link https://www.amazon.com/gp/product/B077XPSKQD/ref=ppx_yo_dt_b_asin_title_o09_s00?ie=UTF8&psc=1

straight to the A0 and gnd of audrino. However the speaker is really low even tho i have max volume on my phone.



(http://i.imgur.com/Aop9U1r.png)
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Mar 01, 2019, 10:40 am
Yes that circuit will show nothing until the audio input exceeds the forward voltage of the diode, which is 0.75V. As it only works in the positive peaks then it needs at least a 1.5V peak to peak audio signal before you get any readings.

That resistor is 100k.

The circuit is a peak detector, you won't get any audio through it only a reading of the peak amplitude, useful for volume measurements, and beat detection.
Title: Re: Real Time Audio Processing
Post by: zkhan on Mar 01, 2019, 02:13 pm
Do you know how to go about a circuit for orientated for audio . Thank you
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Mar 01, 2019, 02:33 pm
I think we are going round in circles, for a circuit for coupling audio into the Arduino see the link in reply #13.
Title: Re: Real Time Audio Processing
Post by: zkhan on Mar 01, 2019, 02:42 pm
"The audio input signal is connected via a 10uF capacitor to the the analog input 1 of the Arduino Board. Two resistors and a trimmpot are adding an DC offset to the audio signal . A potentiometer connected to analog input 0 will be used to control the audio effects."

Unfortunately, I am having a hard time making out the circuit.  Would it be like the png below?
Title: Re: Real Time Audio Processing
Post by: zkhan on Mar 01, 2019, 03:08 pm
Moreover, i tried the circuit below but the noise has gotten worse than using a mic. Furthermore, I have to max the volume of my phone to hear very quiet noise from the speaker. Any suggestions?
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Mar 01, 2019, 04:32 pm
Quote
Any suggestions?
What are the numbers you get from the analogue read?

With that circuit they should be centered about 512 and go up and down from that. If you are getting sound then the audio signal is not big enough.

Quote
, I have to max the volume of my phone to hear very quiet noise from the speaker.
Yes, not a surprise. Fit an operational amplifier with a gain of about 50, before the circuit you posted.

About op-amp circuits (https://www.electronics-notes.com/articles/analogue_circuits/operational-amplifier-op-amp/inverting-amplifier.php)
Title: Re: Real Time Audio Processing
Post by: zkhan on Mar 06, 2019, 08:22 am
Thank you for all the help. I had a question however, if for example i increased my sampling rate to 40KHZ, does higher sampling rate cause inaccuracy it terms that its sampling so fast its missing specific signals?
Title: Re: Real Time Audio Processing
Post by: zkhan on Mar 06, 2019, 08:44 am
Also How can I make sure that Im getting the right results from the 10bit ADC. Lets say im sending a 200 HZ analog signal, what value do i expect the ADC to return? Can that be figured out?
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Mar 06, 2019, 09:20 am
You are missing the point. The sample rate has nothing to do with the sample itself. Going faster means you see finer detail of the waveform that might be missed going slower. If you go slower than twice the highest feature in a signal you get aliasing, which means generating false signals.
Also How can I make sure that Im getting the right results from the 10bit ADC. Lets say im sending a 200 HZ analog signal, what value do i expect the ADC to return? Can that be figured out?
The two are not related in any way. With a 10 bit D/A you can get voltages out from 0 to the voltage reference of the D/A, but only if you put numbers into the D/A of 0 to 1023.
Title: Re: Real Time Audio Processing
Post by: zkhan on Mar 06, 2019, 09:15 pm
Thank you for the information. One more question then, is there any problems that can arise with the audrino if for example the sampling rate of the ADC was set to its highest?
Title: Re: Real Time Audio Processing
Post by: DVDdoug on Mar 06, 2019, 10:18 pm
Section 21.1 of the ATmega datasheet (https://www.sparkfun.com/datasheets/Components/SMD/ATMega328.pdf) says you loose resolution above a sample rate of 15kHz.   (That's the sample rate...  Your signal  is limited to half the sample rate.)

Quote
Up to 76.9 kSPS (Up to 15 kSPS at Maximum Resolution)
Your sample rate may also be limited by whatever "processing" you are doing and how fast your program can loop.
Title: Re: Real Time Audio Processing
Post by: zkhan on Mar 08, 2019, 09:33 pm
Thank you for your feedback. I was reading section 21.1, however, i dont fully grasp why the audrino loses resolution if it goes above 15KHZ.
Title: Re: Real Time Audio Processing
Post by: DVDdoug on Mar 08, 2019, 10:16 pm
Quote
Thank you for your feedback. I was reading section 21.1, however, i dont fully grasp why the audrino loses resolution if it goes above 15KHZ
Have you ever used a multimeter or voltmeter?   It might take about one second before the readings stabilizes.   The Arduino ADC is a lot faster but there is still a limit to how-fast it can read.

But again, I'm more worried about the amount of processing you can do in 1/15,000th of a second.     
Title: Re: Real Time Audio Processing
Post by: zkhan on Mar 11, 2019, 05:36 am
Thank you! Also, because the audrino has a 16 MHZ processor it limits sampling rate. For example if the MHZ was higher it would increase the sampling rate. Furthermore, avr takes 13 conversion cycles, similarity , if the cycles took less steps you can increase the sampling rate. Is that correct?


Moreover, what other limitations does the audrino have in terms of an audio system as I made. For example, some constraints perhaps?
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Mar 11, 2019, 10:37 am
Quote
because the audrino has a 16 MHZ processor it limits sampling rate.
The limit on a Uno is because of the maximum speed of the A/D, this is way below what ever limits are imposed by the clock speed. The A/D can be made to go faster with no loss of quality but if it is made to go too fast the sample becomes increasing noisy.

Quote
Furthermore, avr takes 13 conversion cycles, similarity , if the cycles took less steps you can increase the sampling rate. Is that correct?
Yes, the type of A/D converter used is known as a successive approximation type. Other types exists some that use fewer cycles and others that use less. The type known as a flash converter is fastest requiring only one cycle and is used for video but these are expensive and most are restricted to just 8 bits.

Quote
Moreover, what other limitations does the audrino have in terms of an audio system as I made
Lack of RAM limits sample buffer sizes to just fractions of a second.

Happy conclusion - I assume this is for your report.
Title: Re: Real Time Audio Processing
Post by: zkhan on Mar 11, 2019, 06:42 pm
I am asking because maybe another micro controller will be better for this type of job for example like the Arduino Due or the ras pi mcu.
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Mar 11, 2019, 10:00 pm
Quote
example like the Arduino Due or the ras pi mcu.
Due yes, Raspberry Pi no as it is not a real time operating system so doing audio requires I2S drivers and external sound card.
Title: Re: Real Time Audio Processing
Post by: zkhan on Mar 12, 2019, 06:55 am
Ohh okay. Thank you. I had one quick question, so previously we were talking about the circuit to take the input of the 3.5 analog signal to the audrino. I understand the op amp however i didn't fully understand the purpose of the two resistors and the capacitor. Any help, i believe that in was in the previously page. Thank you.
Title: Re: Real Time Audio Processing
Post by: Grumpy_Mike on Mar 12, 2019, 10:43 am
Quote
however i didn't fully understand the purpose of the two resistors and the capacitor.
It is to bias the audio signal to the mid point of the D.C. range of the A/D converter in the Arduino.

An audio signal is AC, it has positive and negitave peaks. The Arduino can not handle negitave voltages so you have to bias or shift up the D.C. value to half the supply rail with resistors. Then to stop the output device shorting out this bias you couple into this with a capacitor. This is called AC coupling, but just means connected with a seriese capacitor.