Another sound project. Best method?

sweller

New Member
Hi, folks.

I'm in the process of converting a (mostly) working project from a BS2 to an 08M2, and I'm seeking suggestions as to how this might best be done. The BS2 can only do one thing at a time, so it's a kluge at best.

I'm counting pulses from a microphone, and using the value of the pulse count to toggle one of 15 pins which determines the output frequency of a tone generator. The pulses are very narrow bandwidth (0-20 Hz) and it doesn't need accuracy.

Is the 08M2 is fast enough to let me eliminate the tone generator altogether? Actually I think the real question is, can an 08M2 count pulses and generate tones simultaneously - better that a Stamp? The Stamp can't without skipping badly.

How would you code these two tasks for maximum speed, please?
 
Last edited:

AllyCat

Senior Member
Hi,

Welcome to the forum. Generally, the "overhead" of the PICaxe internal Basic interpreter makes it too slow for any real-time audio-type applications. Typically it runs between 100 and 1,000 times slower than assembler or compiled code.

The pulses are very narrow bandwidth (0-20 Hz) and it doesn't need accuracy.
I don't understand what you mean by this. Pulses are not normally considered to have a "bandwith" because they contain multiple harmonics, and "zero bandwidth" is accurate (or meaningless). :confused: Do you mean a "repetition rate" of 0 to 20 Hz, i.e. pulses spaced by a minimum of 50 ms and a maximum of "infinity" (period = 1 / frequency) ?

However, the PICaxe hardware does have multiple counter/timers (giving programmable-frequency PWM outputs, for example), so a PICaxe might be satisfactory if you really do only need to measure/update any parameters every 50 ms or slower.

Cheers, Alan.
 

erco

Senior Member
... and using the value of the pulse count to toggle one of 15 pins...
Of course, the 08M2 doesn't have 15 output pins. A 20M2 is closer to a BS2, note that pin C.6 is input only, so your mike input goes there.

You can run a PicAxe at multiple frequencies, some faster than a BS2; it is certainly up to counting your microphone pulses. But the M2 stumbles when producing sound. Everything stops while a tone is produced, so you may still need a seperate tone generator unless you can live with intermittent sound pulses.
 

sweller

New Member
Hi, Allen. Thanks for the welcome.

Sorry. I obviously misspoke. The output from the microphone is sent through a low-pass Butterworth filter with an Fc of 20 Hz, so I'm only concerned with frequencies below that.

Measure/updates at a rate of 50 ms works very well on the BS2.

But what's the fastest executable command? PULSIN, COUNT, SETTIMER COUNT or?

SETTIMER is not available on the Stamp and I'm intrigued:

settimer count 65535 ; settimer to count mode

main: pause 10000 ; wait 10 seconds, counting pulses
debug ; display timer value
goto main ; loop
 

sweller

New Member
Of course, the 08M2 doesn't have 15 output pins. A 20M2 is closer to a BS2, note that pin C.6 is input only, so your mike input goes there.

You can run a PicAxe at multiple frequencies, some faster than a BS2; it is certainly up to counting your microphone pulses. But the M2 stumbles when producing sound. Everything stops while a tone is produced, so you may still need a seperate tone generator unless you can live with intermittent sound pulses.
Hi, Erco. You just rained on my parade. Getting rid of the tone generator was the biggest priority. I was hoping to be able to continuously send a tone directly to a speaker while the Picaxe is counting pulses. No can do?
 

sweller

New Member
Which could of course be an(other) 08M2
I had thought of that. It would still be cheaper, smaller, and probably faster than a BS2 with a tone generator. But that would mean using SERIN/SEROUT. Yes? Aren't they notoriously slow to execute?
 

eclectic

Moderator
Idle thoughts .......
Using Alan's post #2 suggestion.

28X2 (lots of I/O pins)

Tone generation = PWMout

e
 

AllyCat

Senior Member
Hi,

But what's the fastest executable command? PULSIN, COUNT, SETTIMER COUNT or?

SETTIMER is not available on the Stamp and I'm intrigued:
It's not available on PICaxe M2s either!

The normal counter/timer period is 10 us which can be over- (or under-) clocked by 8 or 16 times. But at the normal clock rate the 16-bit timer will overflow or timeout after 655 ms.

But what are you actually trying to measure, the period between zero-crossings for example? The PICaxe chips have on-chip comparators if that helps. Also the "Timer 1 Gate Control" (and the Capture/Compare latches) can be accessed via the Special Function Registers for more sophisticated timing requirements.

Cheers, Alan.
 

sweller

New Member
Quick way. What frequency is required?

Use the Pwmout Wizard. :))

e
0, and 31 through 40 Hz.

IOW - I'm artificially shifting frequencies up.

I started out with 21 through 30. I was astonished that my headphones can easily reproduce 21 Hz. It's actually pretty brutal, too.
 

AllyCat

Senior Member
Hi,

As many pulses as can be practically counted during a 500 ms period. I can get down to 8 with a BS2,
PICaxe hardware can do about 50,000 in that time. But I don't see how you get those "through a low-pass Butterworth filter with an Fc of 20 Hz". :confused:

Basically, the PWM continues as a (hardware) background task, once it has been setup. But it does have a LOW frequency limit (which you might be able to lower with SFR access or underclocking).

Cheers, Alan.
 

rossko57

Senior Member
Re: using second 08M2 as tone generator ...
But that would mean using SERIN/SEROUT. Yes? Aren't they notoriously slow to execute?
use background HSERIN/OUT ?

Not compelled to use serial at all - you mentioned 15 tones; that encodes neatly into 4 bits and also allows "0" for off. The tone generator could read 4 input data lines to get a value 0-15 at any time. You can squeeze 4-in and 1-out (for the tone) from an 08M2, but would also need to allow 4 output pins for this command purpose on the 'host'

As many pulses as can be practically counted during a 500 ms period
Thousands. See COUNT
http://www.picaxe.com/BASIC-Commands/Digital-InputOutput/count/
 

sweller

New Member
Hi,

PICaxe hardware can do about 50,000 in that time. But I don't see how you get those "through a low-pass Butterworth filter with an Fc of 20 Hz". :confused:

Basically, the PWM continues as a (hardware) background task, once it has been setup. But it does have a LOW frequency limit (which you might be able to lower with SFR access or underclocking).

Cheers, Alan.
Either I'm failing miserably to explain what I'm doing, or my math is flawed. There's a good probability both are true.

Digikey carries a microphone with a "claimed" response of 0-20K Hz. Whether true or not, I bought a couple. Knowing I didn't care about anything above 20 Hz, I thought it prudent to exclude it from the task of analyzing it. So I used a calculator which gave me the RC values needed to construct an active filter.

I'm just trying to count as many subsonic pulses as I can with a microphone.
 

sweller

New Member
Re: using second 08M2 as tone generator ...

use background HSERIN/OUT ?
That's new to me. I'll give it a shot and I hope it works! Else...

You can squeeze 4-in and 1-out (for the tone) from an 08M2, but would also need to allow 4 output pins for this command purpose on the 'host'
This may have to do, but there will still be a break in the output tone when the Picaxe reads the state of the pins?
 

AllyCat

Senior Member
but there will still be a break in the output tone when the Picaxe reads the state of the pins?
No, because the PICaxe (whether being used as a "master" or a "slave") generates PWM using dedicated hardware, so the "tone" can continue when the processor is doing other things. For very low audio frequencies the PICaxe may even be able to monitor the tone to avoid a major phase change (a "click" in the tone) when it changes the frequency.

It would help if you explained what is causing these "subsonic pulses" that you're trying to count (and why). Thunder, doors slamming, gunshots, an eathquake?

The general definition of a "pulse" is that it has a leading edge and a trailing edge. But if you have a (functional) 20 Hz LP filter then you can't expect to count more than 10 of those in 500 ms. Conversely, if you only "look" for 500 ms, you can't detect anything below about 1 or 2 Hz (even if your microphone could detect it).

The normal detector for "subsonic" air-pressure waves is a barometer (or barometric altimeter) ! Electronic modules are available quite cheaply with a data bus compatible with PICaxe. They can be incredibly sensitive to pressure changes, but would normally be designed to "ignore" sound waves.

EDIT: Having "slept" on the problem, are you actually trying to read an ANALOGUE input signal and "analyse" it within the PICaxe? If so, you have confused us all with references to PULSIN and COUNT, etc. commands, which are not relevant to processing analogue signals. Similarly, is your output "tone" just a very low frequency "square wave", or something else? Very low frequency "audio" processing may be possible, even with a PICaxe, but you need to specify exactly what you are trying to achieve, using more specific terms than "pulse" and "tone".

Cheers, Alan.
 
Last edited:

sweller

New Member
EDIT: Having "slept" on the problem, are you actually trying to read an ANALOGUE input signal and "analyse" it within the PICaxe? If so, you have confused us all with references to PULSIN and COUNT, etc. commands, which are not relevant to processing analogue signals. Similarly, is your output "tone" just a very low frequency "square wave", or something else? Very low frequency "audio" processing may be possible, even with a PICaxe, but you need to specify exactly what you are trying to achieve, using more specific terms than "pulse" and "tone".
I apologize emphatically for having caused any confusion.

PULSIN and COUNT are the only relevant commands in PICBASIC. Stamps only have DIGITAL inputs. What I'm using as input signals are changes in the state of a logic level.

Stamps don't know or care what the source of input is, as long as the change in state is large enough to be recognizable as the difference between a 0 and 1, and not so large that it destroys the input. I assume the same is true of a Picaxe.

As far as I can tell with my limited knowledge of the Picaxe, I can attached the same circuit to the same physical input pin (C.1) and the difference between ANALOG and DIGITAL is determined by commands (adcsetup and readadc or input C.1) issued in the program. Am I wrong on that point?

What "exactly" I was trying achieve in coming here was to ask what the most programmatically efficient way of doing with a Picaxe what I'm already doing with a Stamp. Otherwise, I'm feeling that I may not yet have enough experience with the Picaxe to successfully convey my intent (adcsetup/readadc are brand new to me). I'll be back after I've had an opportunity to play with some of Picaxe's unique features.

Kind regards...
 

geoff07

Senior Member
I think it would help enormously if you stated the problem from first principles. It seems that a Picaxe could perhaps do the job quite differently from a BS2. Attempting to port code from a limited chip to one with more and different capabilities could be a waste of time, when different ideas would get you a solution more directly.
 

rossko57

Senior Member
Part of the difficulty is in understanding what you mean by "pulse". So you have an digital input signal in the 0 - 20Hz range....
Is a "pulse" any single lo-hi-lo transition?
Or is a "pulse" a group or burst of transitions, with silence inbetween groups?
 

AllyCat

Senior Member
Hi,

To expand on the comments in the last three posts (and some before), take a look at the following waveform - how many "pulses" does it contain? Admittedly, it is an "artificial" waveform* (generated by a computer), but it is very typical of an audio (or sound) waveform:

Fourier2.jpg

The digital input you have described would normally be assumed to respond at the centre line (green) and thus you would detect one "pulse". But if the "digital threshold" were higher (red) or lower (yellow) you would detect two pulses.

Furthermore, you stated that your frequency range is 0 - 20Hz, but "zero frequency" is "d.c." so any of those thresholds might occur! However, even if your minimum frequency were (say) 1 Hz, then those green, red and yellow levels actually might be various stages (phases) of a 1 Hz signal.

But it gets worse: If you ask people how may pulses (cycles) are in the above waveform, they may well answer "3". And, if you repeatedly applied that signal to a loudspeaker (or headphones) 100 times per second, then nearly everyone will "hear" 300 Hz, not 100 or 200 Hz !
_____

Now look at the second waveform. It looks quite different, but applied to a loudspeaker it will sound exactly the same, i.e. it is "acoustically identical". In this case your "digital threshold" centre-line (green) would detect 3 "pulses", but this would fall to only one if a "d.c. component" (or low frequency) were added to the waveform.

Fourier3.jpg

It may be that this is not relevant to your particular application. But on the information that you have given us so far, it definitely IS !

*For those interested, I created the waveforms from the free software demo here (it needs shockwave flash installed), although I would really like to have found a free application that would show even harmonics as well.

Cheers, Alan.
 
Top