Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: A Question about Peak

  1. #1

    A Question about Peak

    Hey guys,
    I have a question about the Peak function from the audio library,
    I am attaching the code here for reference :

    This is analyse_peak.h :

    Code:
    #ifndef analyze_peakdetect_h_
    #define analyze_peakdetect_h_
    
    #include "Arduino.h"
    #include "AudioStream.h"
    
    class AudioAnalyzePeak : public AudioStream
    {
    public:
    	AudioAnalyzePeak(void) : AudioStream(1, inputQueueArray) {
    		min_sample = 32767;
    		max_sample = -32768;
    	}
    	bool available(void) {
    		__disable_irq();
    		bool flag = new_output;
    		if (flag) new_output = false;
    		__enable_irq();
    		return flag;
    	}
    	float read(void) {
    		__disable_irq();
    		int min = min_sample;
    		int max = max_sample;
    		min_sample = 32767;
    		max_sample = -32768;
    		__enable_irq();
    		min = abs(min);
    		max = abs(max);
    		if (min > max) max = min;
    		return (float)max / 32767.0f;
    	}
    	float readPeakToPeak(void) {
    		__disable_irq();
    		int min = min_sample;
    		int max = max_sample;
    		min_sample = 32767;
    		max_sample = -32768;
    		__enable_irq();
    		return (float)(max - min) / 32767.0f;
    	}
    
    	virtual void update(void);
    private:
    	audio_block_t *inputQueueArray[1];
    	volatile bool new_output;
    	int16_t min_sample;
    	int16_t max_sample;
    };
    
    #endif
    and this is analyse_peak.cpp :

    Code:
    #include "analyze_peak.h"
    
    void AudioAnalyzePeak::update(void)
    {
    	audio_block_t *block;
    	const int16_t *p, *end;
    	int32_t min, max;
    
    	block = receiveReadOnly();
    	if (!block) {
    		return;
    	}
    	p = block->data;
    	end = p + AUDIO_BLOCK_SAMPLES;
    	min = min_sample;
    	max = max_sample;
    	do {
    		int16_t d=*p++;
    		// TODO: can we speed this up with SSUB16 and SEL
    		// http://www.m4-unleashed.com/parallel-comparison/
    		if (d<min) min=d;
    		if (d>max) max=d;
    	} while (p < end);
    	min_sample = min;
    	max_sample = max;
    	new_output = true;
    	release(block);
    }
    My question is this : How is it possible for min_sample and max_sample to ever be updated ?
    As one can see they are both initialized in the constructor to

    Code:
    min_sample = 32767;
    max_sample = -32768;
    and the update function only updates them if they achieve greater or smaller values which seems impossible...
    what am I missing here ?

    thanks in advance, A.H.

  2. #2
    Sorry if this is a trivial question,
    I will be glad to any answer...

  3. #3
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,259
    Quote Originally Posted by Avi Harel View Post
    Sorry if this is a trivial question,
    I will be glad to any answer...
    for getting the minimal value, you first initialize with overall max value, so that all other values are smaller.
    same with maximal value, that you initialize with overall minimal value

  4. #4
    Quote Originally Posted by WMXZ View Post
    for getting the minimal value, you first initialize with overall max value, so that all other values are smaller.
    same with maximal value, that you initialize with overall minimal value
    thanks...for some reason I saw it reversed,don't know how I could have missed that...

  5. #5
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,259
    Quote Originally Posted by Avi Harel View Post
    thanks...for some reason I saw it reversed,don't know how I could have missed that...
    Alternative would be to set min/max value to first value encountered. This however, may not always be possible.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •