Hash :
5809d5b5
Author :
Date :
2021-12-22T15:41:29
LANCIR 3.0.1. A deep algorithm's redesign. Now features AVX, SSE2, and NEON optimizations out-of-the box. Now it is likely one of the fastest (if not the fastest) Lanczos algorithms available for CPUs. NOTE: resizeImage() function's arguments were changed: now also accepting NewBuf's scanline size.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
// Resizing frequency response and SNR estimation test.
#include <stdio.h>
#include "../avir.h"
#include "../lancir.h"
#include "../../../../libvox/Sources/Core/CWorkerThreadPool.h"
using namespace vox;
#define IS_AVIR 1 // Use AVIR resizer, LancIR otherwise.
#define IS_UPS 1 // Perform upsampling, downsampling otherwise.
#if IS_AVIR
#define NAME "AVIR29"
#else // IS_AVIR
#define NAME "Lanc3"
#endif // IS_AVIR
const double Bias = 0.0;
const double SizeCoeff = 0.3;
const int SrcImageWidth = 1024 * 16;
const int SrcImageHeight = 12;
const int Offs = 32;
const int ImgCapacity = SrcImageWidth * SrcImageHeight;
CFixedBuffer< float > SrcImg( ImgCapacity );
avir :: CImageResizerParamsDef ResizeParams;
double p1g;
class CStats
{
public:
CSyncObject StateSync;
double avgd; // Frequency response.
double avgd2; // Dynamic range (obtained via two-way resizing).
double peakd; // Peak error (obtained via two-way resizing).
int avgc;
CStats()
{
reset();
}
void reset()
{
avgd = 0.0;
avgd2 = 0.0;
peakd = 0.0;
avgc = 0;
}
};
inline double calcRMS( const float* p, const int l )
{
double s = 0.0;
int i;
for( i = 0; i < l; i++ )
{
s += sqr( p[ i ] - Bias );
}
return( sqrt( s / l ));
}
inline double calcRMSDiff( const float* const p1, const float* const p2,
const double p2g, const int l, double& peakd )
{
peakd = 0.0;
double s = 0.0;
int i;
for( i = 0; i < l; i++ )
{
const double d = ( p1[ i ] - Bias ) * p1g - ( p2[ i ] - Bias ) * p2g;
s += sqr( d );
peakd = max( peakd, fabs( d ));
}
return( sqrt( s / l ));
}
class CResampThread : public CWorkerThread
{
public:
CStats* Stats; ///< Pointer to summary statistics object.
///<
double k; ///< Resize factor.
///<
int BitDepth; ///< Resize bit depth.
///<
protected:
virtual ecode performWork()
{
#if IS_AVIR
avir :: CImageResizer<> ImageResizer( BitDepth, 0, ResizeParams );
#else // IS_AVIR
avir :: CLancIR ImageResizer;
#endif // IS_AVIR
const int DstImageWidth = (int) ceil( SrcImageWidth / k );
const int DstImageHeight = (int) ceil( SrcImageHeight / k );
CFixedBuffer< float > DstImg( DstImageWidth * DstImageHeight );
#if IS_AVIR
ImageResizer.resizeImage( &SrcImg[ 0 ], SrcImageWidth,
SrcImageHeight, 0, &DstImg[ 0 ], DstImageWidth,
DstImageHeight, 1, -k );
#else // IS_AVIR
ImageResizer.resizeImage( &SrcImg[ 0 ], SrcImageWidth,
SrcImageHeight, 0, &DstImg[ 0 ], DstImageWidth,
DstImageHeight, 0, 1, -k, -k, 0.0, 0.0 );
#endif // IS_AVIR
CFixedBuffer< float > DstImg2( SrcImageWidth * SrcImageHeight );
#if IS_AVIR
ImageResizer.resizeImage( &DstImg[ 0 ], DstImageWidth,
DstImageHeight, 0, &DstImg2[ 0 ], SrcImageWidth,
SrcImageHeight, 1, -1.0 / k );
#else // IS_AVIR
ImageResizer.resizeImage( &DstImg[ 0 ], DstImageWidth,
DstImageHeight, 0, &DstImg2[ 0 ], SrcImageWidth,
SrcImageHeight, 0, 1, -1.0 / k, -1.0 / k, 0.0, 0.0 );
#endif // IS_AVIR
const double r = calcRMS( &DstImg[ Offs ], DstImageWidth - Offs * 2 );
const double p2g = 1.0 / calcRMS( &DstImg2[ Offs ],
SrcImageWidth - Offs * 2 );
double peakd = 0.0;
const double r2 = calcRMSDiff( &SrcImg[ Offs ], &DstImg2[ Offs ],
p2g, SrcImageWidth - Offs * 2, peakd );
VOXSYNC( Stats -> StateSync );
Stats -> avgd += r * r;
Stats -> avgd2 += r2 * r2;
Stats -> peakd = max( Stats -> peakd, peakd );
Stats -> avgc++;
VOXRET;
}
};
int main()
{
int i;
CWorkerThreadPool Threads;
for( i = 0; i < CSystem :: getProcessorCount(); i++ )
{
Threads.add( new CResampThread() );
}
const double minf = 0.01;
#if IS_UPS
const double maxf = 0.99;
#else // IS_UPS
const double maxf = 0.99 * SizeCoeff;
#endif // IS_UPS
const int c = 128;
int k;
printf( "\t%s %s\t%s %s\t%s %s\n",
NAME, " FR", NAME, " DR", NAME, " PE" );
for( k = 0; k < c; k++ )
{
const double th = M_PI * exp( log( minf ) +
log( maxf / minf ) * k / ( c - 1 ));
// Prepare image with sinewave content that corresponds to the
// "th" circular frequency.
int j;
for( j = 0; j < SrcImageHeight; j++ )
{
double s = 0.0;
for( i = 0; i < SrcImageWidth; i++ )
{
SrcImg[ j * SrcImageWidth + i ] = (float) cos( i * th );
s += SrcImg[ j * SrcImageWidth + i ];
}
s /= SrcImageWidth;
double s2 = 0.0;
// Perform de-biasing.
for( i = 0; i < SrcImageWidth; i++ )
{
SrcImg[ j * SrcImageWidth + i ] -= s;
s2 += sqr( SrcImg[ j * SrcImageWidth + i ]);
}
// Perform power normalization, and add a constant Bias value.
s2 = 1.0 / sqrt( s2 / SrcImageWidth );
for( i = 0; i < SrcImageWidth; i++ )
{
SrcImg[ j * SrcImageWidth + i ] *= s2;
SrcImg[ j * SrcImageWidth + i ] += Bias;
}
}
p1g = 1.0 / calcRMS( &SrcImg[ Offs ], SrcImageWidth - Offs * 2 );
// Perform multi-threaded resizing and statistics accumulation over
// a logarithmically-spaced resizing "k" factors.
CStats Stats;
double k = 1.0;
while( k > SizeCoeff )
{
CResampThread* rs;
VOXERRSKIP( Threads.getIdleThread( rs ));
rs -> Stats = &Stats;
#if IS_UPS
rs -> k = k;
#else // IS_UPS
rs -> k = 1.0 / k;
#endif // IS_UPS
rs -> BitDepth = 16;
rs -> start();
k *= 0.95;
}
VOXERRSKIP( Threads.waitAllForFinish() );
Stats.avgd = 10.0 * log( Stats.avgd / Stats.avgc ) / log( 10.0 );
Stats.avgd2 = 10.0 * log( Stats.avgd2 / Stats.avgc ) / log( 10.0 );
Stats.peakd = 20.0 * log( Stats.peakd ) / log( 10.0 );
printf( "%.6f\t%.6f\t%.6f\t%.6f\n", th / M_PI,
Stats.avgd, Stats.avgd2, Stats.peakd );
}
}