Exemplo n.º 1
0
void
AAKR::normalize(Math::Matrix& mean, Math::Matrix& std)
{
    // Resize mean and standard deviation variables.
    mean.resizeAndFill(1, sampleSize(), 0);
    std.resizeAndFill(1, sampleSize(), 0);

    // Compute mean.
    for (unsigned i = 0; i < sampleSize(); i++)
        mean(i) = sum(m_data.get(0, m_num_values - 1, i, i)) / m_num_values;

    // Compute standard deviation.
    for (unsigned j = 0; j < sampleSize(); j++)
    {
        double sum = 0;

        // Sum of the power of two difference
        // between the value and the mean.
        for (unsigned i = 0; i < m_num_values; i++)
            sum += std::pow(m_data(i, j) - mean(j), 2);

        // Standard deviation.
        std(j) = std::sqrt(sum / m_num_values);

        // Normalize each member of the data set.
        for (unsigned i = 0; i < m_num_values; i++)
        {
            if (std(j))
                m_norm(i, j) = (m_data(i, j) - mean(j)) / std(j);
            else
                m_norm(i, j) = 0;
        }
    }
}
Exemplo n.º 2
0
static int lua_stoprec_DSP(lua_State *L){
	int argc = lua_gettop(L);
	#ifndef SKIP_ERROR_HANDLING
	if (argc != 0) return luaL_error(L, "wrong number of arguments");
	#endif
	bool isSampling;
	MICU_IsSampling(&isSampling);
	u32 offset;
	if (isSampling){
		offset = micGetLastSampleOffset();
		MICU_StopSampling();
	}else offset = micsize;
	u32 samplerate = sampleSize(srate);
	u32 mute_size = samplerate<<1;
	u8* bytebuf = (u8*)linearAlloc(offset);
	memcpy(bytebuf, (u8*)micbuf, offset);
	micExit();
	free(micbuf);
	Music* songFile = (Music*)malloc(sizeof(Music));
	songFile->audiobuf = bytebuf;
	songFile->audiobuf2 = NULL;
	songFile->big_endian = false;
	songFile->mem_size = 0;
	songFile->ch = 0xDEADBEEF;
	songFile->size = offset;
	songFile->samplerate = samplerate;
	songFile->encoding = CSND_ENCODING_PCM16;	
	strcpy(songFile->author,"");
	strcpy(songFile->title,"");
	songFile->isPlaying = false;
	songFile->bytepersample = 2;
	songFile->magic = 0x4C534E44;
	lua_pushinteger(L,(u32)songFile);
	return 1;
}
Exemplo n.º 3
0
Math::Matrix
AAKR::estimate(Math::Matrix query, double variance)
{
    Math::Matrix mean;
    Math::Matrix std;

    normalize(mean, std);

    // Use normalized query vector.
    computeDistance((query - mean) / std);
    computeWeights(variance);

    double s = sum(m_weights);

    // Avoid division by zero.
    if (!s)
        return query;

    // Combine with weights.
    Math::Matrix result = ((transpose(m_weights) * m_norm) / s);

    // Normalize.
    for (unsigned i = 0; i < sampleSize(); i++)
        result(i) = result(i) * std(i) + mean(i);

    return result;
}
/*!
    Returns the number of bytes required to represent one frame (a sample in each channel) in this format.

    Returns 0 if this format is invalid.
*/
int QAudioFormat::bytesPerFrame() const
{
    if (!isValid())
        return 0;

    return (sampleSize() * channelCount()) / 8;
}
Exemplo n.º 5
0
void
AAKR::add(Math::Matrix v)
{
    if (dataSize() == 0)
        throw std::runtime_error("unable to add: data window size is undefined.");

    if (v.rows() != 1)
        throw std::runtime_error("unable to add: new sample is not a row vector.");

    if (sampleSize() == 0)
        m_data.resize(dataSize(), v.columns());

    if ((unsigned)v.columns() != sampleSize())
        throw std::runtime_error("unable to add: sample size does not match.");

    // Write to the data set.
    m_data.set(m_index, m_index, 0, sampleSize() - 1, v);

    // Increment data set index.
    increment();
}
Exemplo n.º 6
0
void
AAKR::resize(unsigned r, unsigned c)
{
    m_index = 0;
    m_num_values = 0;

    if (r == dataSize() && c == sampleSize())
        return;

    m_data.resizeAndFill(r, c, 0.0);
    m_norm = m_data;
    m_distances.resizeAndFill(r, 1, 0.0);
    m_weights.resizeAndFill(r, 1, 0.0);
}
Exemplo n.º 7
0
void
AAKR::computeDistance(Math::Matrix query)
{
    if (query.rows() != 1)
        throw std::runtime_error("unable to compute distance: reference is not row vector.");

    if ((unsigned)query.columns() != sampleSize())
        throw std::runtime_error("unable to compute distance: sample size does not match.");

    m_distances.fill(0.0);

    // Fill distances vector.
    for (unsigned i = 0; i < m_num_values; i++)
    {
        Math::Matrix q = query - m_norm.row(i);
        m_distances(i) = std::sqrt(sum(q * transpose(q)));
    };
}
Exemplo n.º 8
0
/**
 * Checks if the Haar-like features generated by haargen.cpp conform to Pavani's restrictions.
 */
int main(int argc, char * args[])
{
    if (argc != 2) {
        return 1;
    }

    cv::Size sampleSize(SAMPLE_SIZE, SAMPLE_SIZE); //size in pixels of the trainning images


    //load the wavelets
    std::vector<HaarWavelet> wavelets;
    std::cout << "Loading Haar wavelets from " << args[1] << std::endl;
    loadHaarWavelets(args[1], wavelets);
    std::cout << "Loaded " << wavelets.size() << " wavelets." << std::endl;

    //STATS
    int dimensions[3] = {0, 0, 0}; //2, 3 and 4 dimensions of the wavelets
    int regionsHistogram[3][3]; //x regions are 8 - 4 - 8
                                //y regions are 7 - 6 - 7
    int widthHistogram[20], heightHistogram[20];
    int totalRectangles;
    {
        for(int i = 0; i < 20; ++i)
        {
            widthHistogram[i] = heightHistogram[i] = 0;
        }
        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                regionsHistogram[i][j] = 0;
            }
        }


        std::vector<HaarWavelet>::iterator it = wavelets.begin();
        const std::vector<HaarWavelet>::iterator end = wavelets.end();
        for(;it != end; ++it)
        {
            dimensions[it->dimensions() - 2]++;

            std::vector<cv::Rect>::const_iterator itr = it->rects_begin();
            const std::vector<cv::Rect>::const_iterator endr = it->rects_end();
            for(; itr != endr; ++itr)
            {
                const cv::Rect r = *itr;
                totalRectangles++;
                widthHistogram[r.width - 1]++;
                heightHistogram[r.height - 1]++;

                float horizontalMean = r.x + r.width / 2.0f;
                float verticalMean = r.y + r.height / 2.0f;

                const int xIndex = horizontalMean < 8 ? 0 :
                              8 <= horizontalMean && horizontalMean < 12 ? 1 :
                                                                           2;
                const int yIndex = verticalMean < 7 ? 0 :
                              7 <= verticalMean && verticalMean < 13 ? 1 :
                                                                       2;
                regionsHistogram[xIndex][yIndex]++;
            }
        }

        std::cout << "Total 2D/3D/4D wavelets: " << dimensions[0] << "/" << dimensions[1] << "/" << dimensions[2] << std::endl;
        std::cout << "Total rectangles: " << totalRectangles << std::endl;

        std::stringstream wHist, hHist;
        wHist << "Width histogram: ";
        hHist << "Height histogram: ";
        for(int i = 0; i < 20; ++i)
        {
            wHist << widthHistogram[i] << " ";
            hHist << heightHistogram[i] << " ";
        }
        std::cout << wHist.str() << std::endl;
        std::cout << hHist.str() << std::endl;

        std::cout << "Rectangles mean position 2D histogram: " << std::endl;
        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                std::cout << regionsHistogram[j][i] << " ";
            }
            std::cout << std::endl;
        }
    }



    {//double checks for wavelets with overlapping rectangles
       std::cout << "Checking for overlapped rectangles..." << std::endl;
       std::vector<HaarWavelet>::iterator it = wavelets.begin();
       const std::vector<HaarWavelet>::iterator end = wavelets.end();
       for(;it != end; ++it)
       {
           if (hasOverlappingRectangles(*it))
           {
               std::cout << "Overlaps ==> ";
               it->write(std::cout);
               std::cout << std::endl;
           }
       }
    }



    {//double checks if any rect has x or y at position 20 or more
        std::cout << "Checking for problems with rectangle sizes..." << std::endl;
        std::vector<HaarWavelet>::iterator it = wavelets.begin();
        const std::vector<HaarWavelet>::iterator end = wavelets.end();
        for(;it != end; ++it)
        {
            std::vector<cv::Rect>::const_iterator itr = it->rects_begin();
            const std::vector<cv::Rect>::const_iterator endr = it->rects_end();
            for(; itr != endr; ++itr)
            {
                if (itr->x >= 20 || itr->y >= 20 || itr->x < 0 || itr->y < 0 || itr->x + itr->width > 20 || itr->y + itr->height > 20 || itr->width < 3 || itr->height < 3)
                {
                    std::cout << "Size problem ==> ";
                    it->write(std::cout);
                    std::cout << std::endl;
                    break;
                }
            }
        }
    }



    {//double checks for repeated rects in a haar wavelet through brute force
        std::cout << "Checking duplicated rects in each haar wavelet..." << std::endl;
        std::vector<HaarWavelet>::iterator it = wavelets.begin();
        const std::vector<HaarWavelet>::iterator end = wavelets.end();
        for(;it != end; ++it)
        {
            std::vector<HaarWavelet>::iterator it2 = it + 1;
            for(;it2 != end; ++it2)
            {
                if( same(*it, *it2) )
                {
                    std::cout << "Repeats ==> ";
                    it->write(std::cout);
                    std::cout << std::endl;
                }
            }
        }
    }

    return 0;
}
Exemplo n.º 9
0
qint64 OutputALSA::latency()
{
    return m_prebuf_fill * 1000 / sampleRate() / channels() / sampleSize();
}
Exemplo n.º 10
0
int AudioParameters::sampleSize() const
{
    return sampleSize(m_format);
}