00001 /**************************************************************** 00002 * 00003 * Copyright (c) 2010 00004 * 00005 * Fraunhofer Institute for Manufacturing Engineering 00006 * and Automation (IPA) 00007 * 00008 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00009 * 00010 * Project name: care-o-bot 00011 * ROS stack name: cob_driver 00012 * ROS package name: cob_light 00013 * Description: Switch robots led color by sending data to 00014 * the led-µC over serial connection. 00015 * 00016 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00017 * 00018 * Author: Benjamin Maidel, email:benjamin.maidel@ipa.fraunhofer.de 00019 * Supervised by: Benjamin Maidel, email:benjamin.maidel@ipa.fraunhofer.de 00020 * 00021 * Date of creation: August 2012 00022 * ToDo: 00023 * 00024 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00025 * 00026 * Redistribution and use in source and binary forms, with or without 00027 * modification, are permitted provided that the following conditions are met: 00028 * 00029 * * Redistributions of source code must retain the above copyright 00030 * notice, this list of conditions and the following disclaimer. 00031 * * Redistributions in binary form must reproduce the above copyright 00032 * notice, this list of conditions and the following disclaimer in the 00033 * documentation and/or other materials provided with the distribution. 00034 * * Neither the name of the Fraunhofer Institute for Manufacturing 00035 * Engineering and Automation (IPA) nor the names of its 00036 * contributors may be used to endorse or promote products derived from 00037 * this software without specific prior written permission. 00038 * 00039 * This program is free software: you can redistribute it and/or modify 00040 * it under the terms of the GNU Lesser General Public License LGPL as 00041 * published by the Free Software Foundation, either version 3 of the 00042 * License, or (at your option) any later version. 00043 * 00044 * This program is distributed in the hope that it will be useful, 00045 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00046 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00047 * GNU Lesser General Public License LGPL for more details. 00048 * 00049 * You should have received a copy of the GNU Lesser General Public 00050 * License LGPL along with this program. 00051 * If not, see <http://www.gnu.org/licenses/>. 00052 * 00053 ****************************************************************/ 00054 00055 #include "soundbuffer.h" 00056 #include "stdio.h" 00057 00058 namespace mybeat 00059 { 00060 00061 SoundBuffer::SoundBuffer(uint16_t size) 00062 : m_maxPwr (0.0), m_pwr(0), m_averagePwr(0), m_average(0), m_newDataReady(false) 00063 { 00064 this->m_size=size; 00065 m_Buffer.resize(size,0); 00066 } 00067 00068 int16_t SoundBuffer::average() 00069 { 00070 int32_t sum=0; 00071 for(uint16_t i=0;i<m_size;i++) 00072 { 00073 sum+=m_Buffer[i]; 00074 } 00075 sum/=m_size; 00076 return (int16_t)sum; 00077 } 00078 00079 double SoundBuffer::pwr() 00080 { 00081 double pwr=0; 00082 for(uint16_t i=0; i<m_size; i++) 00083 { 00084 pwr+=(double)m_Buffer[i] * (double)m_Buffer[i]; 00085 } 00086 if(pwr > m_maxPwr) 00087 m_maxPwr = pwr; 00088 return pwr; 00089 } 00090 00091 double SoundBuffer::max_pwr() 00092 { 00093 m_maxPwr = m_maxPwr*0.95; 00094 return m_maxPwr; 00095 } 00096 00097 uint16_t SoundBuffer::average_pwr() 00098 { 00099 uint32_t sum=0; 00100 for(uint16_t i=0;i<m_size;i++) 00101 { 00102 if(m_Buffer[i] < 0) 00103 sum+=-1*m_Buffer[i]; 00104 else 00105 sum+=m_Buffer[i]; 00106 } 00107 sum/=m_size; 00108 return (uint16_t)sum; 00109 } 00110 bool SoundBuffer::write(uint16_t pos,int16_t value) 00111 { 00112 if(pos < m_size) 00113 { 00114 m_Buffer[pos]=value; 00115 return true; 00116 } 00117 else 00118 return false; 00119 } 00120 int16_t SoundBuffer::read(uint16_t pos) 00121 { 00122 return m_Buffer.at(pos); 00123 } 00124 }