Merge pull request #492 from diizy/stable-0.4
GraphModel: fix/improve normalize() function - remove bias before maximi...
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
*
|
||||
* Copyright (c) 2006-2007 Andreas Brandmaier <andy/at/brandmaier/dot/de>
|
||||
* 2008 Paul Giblock <drfaygo/at/gmail/dot/com>
|
||||
*
|
||||
*
|
||||
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
@@ -24,8 +24,8 @@
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _GRAPH_H
|
||||
#define _GRAPH_H
|
||||
#ifndef GRAPH_H
|
||||
#define GRAPH_H
|
||||
|
||||
#include <QtGui/QWidget>
|
||||
#include <QtGui/QPixmap>
|
||||
@@ -71,7 +71,7 @@ public:
|
||||
{
|
||||
return m_graphStyle;
|
||||
}
|
||||
|
||||
|
||||
|
||||
inline void setGraphStyle( graphStyle _s )
|
||||
{
|
||||
@@ -101,10 +101,10 @@ private:
|
||||
|
||||
QPixmap m_foreground;
|
||||
QColor m_graphColor;
|
||||
|
||||
|
||||
graphModel * m_graphModel;
|
||||
graphStyle m_graphStyle;
|
||||
|
||||
|
||||
bool m_mouseDown;
|
||||
int m_lastCursorX;
|
||||
|
||||
@@ -125,7 +125,7 @@ public:
|
||||
virtual ~graphModel();
|
||||
|
||||
// TODO: saveSettings, loadSettings?
|
||||
|
||||
|
||||
inline float minValue() const
|
||||
{
|
||||
return( m_minValue );
|
||||
@@ -140,7 +140,7 @@ public:
|
||||
{
|
||||
return( m_samples.count() );
|
||||
}
|
||||
|
||||
|
||||
inline const float * samples() const
|
||||
{
|
||||
return( m_samples.data() );
|
||||
@@ -160,10 +160,12 @@ public slots:
|
||||
void setWaveToSquare();
|
||||
void setWaveToNoise();
|
||||
QString setWaveToUser( );
|
||||
|
||||
|
||||
void smooth();
|
||||
void smoothNonCyclic();
|
||||
void normalize();
|
||||
void invert();
|
||||
void shiftPhase( int _deg );
|
||||
|
||||
signals:
|
||||
void lengthChanged();
|
||||
|
||||
@@ -629,28 +629,61 @@ void graphModel::smoothNonCyclic()
|
||||
emit samplesChanged(0, length()-1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void graphModel::normalize()
|
||||
{
|
||||
float max = 0.0001f;
|
||||
float avg = 0.0f;
|
||||
|
||||
// first correct dc offset by normalizing to average
|
||||
for( int i = 0; i < length(); i++ )
|
||||
{
|
||||
if( fabsf(m_samples[i]) > max && m_samples[i] != 0.0f )
|
||||
{
|
||||
max = fabs( m_samples[i] );
|
||||
}
|
||||
}
|
||||
avg += m_samples[i];
|
||||
avg /= length();
|
||||
for( int i = 0; i < length(); i++ )
|
||||
m_samples[i] -= avg;
|
||||
|
||||
// then maximize
|
||||
for( int i = 0; i < length(); i++ )
|
||||
max = qMax( max, qAbs( m_samples[i] ) );
|
||||
|
||||
for( int i = 0; i < length(); i++ )
|
||||
{
|
||||
m_samples[i] /= max;
|
||||
}
|
||||
m_samples[i] = qBound( m_minValue, m_samples[i] / max, m_maxValue );
|
||||
|
||||
if( max != 1.0f ) {
|
||||
// signal changes if any
|
||||
if( max != 1.0f || avg != 0.0f )
|
||||
emit samplesChanged( 0, length()-1 );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void graphModel::invert()
|
||||
{
|
||||
const float range = m_maxValue - m_minValue;
|
||||
|
||||
for( int i = 0; i < length(); i++ )
|
||||
m_samples[i] = m_minValue + ( range - ( m_samples[i] - m_minValue ) );
|
||||
|
||||
emit samplesChanged( 0, length()-1 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
void graphModel::shiftPhase( int _deg )
|
||||
{
|
||||
// calculate offset in samples
|
||||
int offset = ( _deg * length() ) / 360; //multiply first because integers
|
||||
|
||||
// store values in temporary array
|
||||
QVector<float> temp = m_samples;
|
||||
|
||||
// shift phase
|
||||
for( int i = 0; i < length(); i++ )
|
||||
m_samples[i] = temp[ ( i + offset ) % length() ];
|
||||
|
||||
emit samplesChanged( 0, length()-1 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
#include "moc_graph.cxx"
|
||||
|
||||
Reference in New Issue
Block a user