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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
/* ------------------------------------------------------------------
libofa -- the Open Fingerprint Architecture library
Copyright (C) 2006 MusicIP Corporation
All rights reserved.
-------------------------------------------------------------------*/
// FILE: "signal_op.cpp"
// MODULE: Implementation for class Signal_op
// AUTHOR: Frode Holm
// DATE CREATED: 1/12/06
#include <math.h>
#include "signal_op.h"
#include "AFLIB/aflibConverter.h"
#include "error_op.h"
Signal_op::Signal_op()
{
Data = 0;
iOwnData = false;
NumChannels = 0;
BufSize = 0;
NumBlocks = 0;
Rate = 0;
}
Signal_op::~Signal_op()
{
if (iOwnData)
delete[] Data;
}
void
Signal_op::Load(short* samples, long size, int sRate, bool stereo)
{
Data = samples;
iOwnData = false;
NumChannels = stereo ? 2 : 1;
BufSize = size;
NumBlocks = BufSize / NumChannels;
Rate = sRate;
}
// CutSignal deletes samples from the sample buffer
void
Signal_op::CutSignal(double start, double dur)
{
int i, n;
short* samples = Data;
long startS = (long)(start * Rate / 1000.0);
long stopS = (long)(startS + dur * Rate / 1000.0);
NumBlocks = (stopS-startS);
if (NumBlocks <= 0)
throw OnePrintError("Programming error: CutSignal");
BufSize = NumBlocks * NumChannels;
short* tmpBuf = new short[BufSize];
startS *= NumChannels;
stopS *= NumChannels;
// Copy to new buffer
for (i=startS, n=0; i<stopS; i++, n++)
tmpBuf[n] = samples[i];
if (iOwnData)
delete[] Data;
Data = tmpBuf;
iOwnData = true;
}
void
Signal_op::PrepareStereo(long newRate, double silTh)
{
// Convert to mono
if (GetCrossCorrelation() < -0.98)
LMinusR();
else
LPlusR();
PrepareMono(newRate, silTh);
}
void
Signal_op::PrepareMono(long newRate, double silTh)
{
RemoveSilence(silTh, silTh);
RemoveDCOffset();
// Check for rate conversion
if (newRate != Rate)
ConvertSampleRate(newRate);
Normalize();
}
// Add left and right channels
void Signal_op::LPlusR()
{
if (NumChannels != 2)
return;
short* tmpBuf = new short[NumBlocks];
short* samples = Data;
for (long i=0, n=0; i<NumBlocks*2; i+=2, n++)
{
int sum = samples[i] + samples[i+1];
tmpBuf[n] = sum / 2;
}
if (iOwnData)
delete[] Data;
Data = tmpBuf;
iOwnData = true;
NumChannels = 1;
BufSize = NumBlocks;
}
// Subtract left and right channels.
void Signal_op::LMinusR()
{
if (NumChannels != 2)
return;
short * tmpBuf = new short[NumBlocks];
short * samples = Data;
for (long i=0, n=0; i<NumBlocks*2; i+=2, n++)
{
int sum = samples[i] - samples[i+1];
tmpBuf[n] = sum / 2;
}
if (iOwnData)
delete[] Data;
Data = tmpBuf;
iOwnData = true;
NumChannels = 1;
BufSize = NumBlocks;
}
void
Signal_op::RemoveSilence(double startTh, double endTh)
{
long i, n;
short* samples = Data;
// Truncate leading and trailing silence
long stop = NumBlocks;
int silBlock = (int) (Rate*2.2/400);
int count = 0;
long sum = 0;
long start = 0;
// Front silence removal
while (start < stop)
{
sum += abs(samples[start]);
count++;
if (count >= silBlock)
{
double av = (double)sum/silBlock;
if (av > startTh)
{
start -= count-1;
break;
}
count = 0;
sum = 0;
}
start++;
}
if (start < 0) start = 0;
// Back silence removal
count = 0;
sum = 0;
while (stop > start)
{
sum += abs(samples[stop-1]);
count++;
if (count >= silBlock)
{
double av = (double)sum/silBlock;
if (av > endTh)
{
stop += count;
break;
}
count = 0;
sum = 0;
}
stop--;
}
if (stop > NumBlocks) stop = NumBlocks;
if (stop-start <= 0)
throw OnePrintError("Signal has silence only", SILENCEONLY);
NumBlocks = (stop-start);
if (NumBlocks <= 0)
throw OnePrintError("Signal is corrupt");
BufSize = NumBlocks;
short* tmpBuf = new short[BufSize];
// Copy to new buffer
for (i=start, n=0; i<stop; i++, n++)
tmpBuf[n] = samples[i];
if (iOwnData)
delete[] Data;
Data = tmpBuf;
iOwnData = true;
}
void
Signal_op::RemoveDCOffset()
{
long len = GetLength();
short* x = Data;
double yn=0, yn1=0;
double sum = 0;
long cnt = 0;
double ramp = 1000.0; // Ramp-up time (ms)
long lim = (long) ((ramp/1000)*GetRate());
double k = 1000.0/(GetRate()*ramp);
double maxP = 0, maxN = 0;
for (long n=1; n<=len; n++)
{
yn = yn1 + k*((double)x[n-1] - yn1);
yn1 = yn;
if (n > lim*3)
{
sum += yn;
cnt++;
}
if (x[n-1] > maxP)
maxP = x[n-1];
if (x[n-1] < maxN)
maxN = x[n-1];
}
double dcOffset = sum/(double)cnt;
// Remove if greater than this
if (fabs(dcOffset) > 15) // otherwise don't bother
{
// Check to se if we have to "denormalize" to make sure there's headroom for the DC removal
double factorP=0, factorN=0, factor=0;
if (maxP - dcOffset > MaxSample)
factorP = ((double)MaxSample - dcOffset) / maxP;
if (maxN - dcOffset < MinSample)
factorN = ((double)MinSample + dcOffset) / maxN;
// only one can apply
if (factorP > 0)
factor = factorP;
else if (factorN > 0)
factor = factorN;
for (long i=0; i<len; i++)
{
double sample = (double)x[i];
if (factor > 0)
sample *= factor;
sample -= dcOffset;
// round sample
if (sample > 0)
x[i] = (short) floor(sample + 0.5);
else
x[i] = (short) ceil(sample - 0.5);
}
}
}
void
Signal_op::Normalize()
{
short* samples = Data;
long i;
int max = 0;
double factor;
for (i=0; i<NumBlocks; i++)
{
if (abs(samples[i]) > max)
max = abs(samples[i]);
}
if (max >= MaxSample) {
factor = 1;
} else {
factor = MaxSample / (double) max;
for (i=0; i<NumBlocks; i++)
{
double tmp = (double)samples[i] * factor;
// round sample
if (tmp > 0)
samples[i] = (short) floor(tmp + 0.5);
else
samples[i] = (short) ceil(tmp - 0.5);
}
}
}
// Mono signals only
void
Signal_op::ConvertSampleRate(long targetSR)
{
if (NumChannels > 1) return;
aflibConverter srConv(true, false, true); // Large filter with coefficients interpolation
double factor = (double)targetSR/(double)Rate;
long tmpSize = (long) (BufSize * factor + 2);
short* tmpBuf = new short[tmpSize];
srConv.initialize(factor, 1);
int inCount = BufSize;
int outCount = (int) (BufSize * factor);
int outRet;
outRet = srConv.resample(inCount, outCount, GetBuffer(), tmpBuf);
if (iOwnData)
delete[] Data;
Data = tmpBuf;
iOwnData = true;
Rate = targetSR;
NumBlocks = BufSize = outRet;
}
double
Signal_op::GetCrossCorrelation()
{
// Cross Channel Correlation - stereo signals only
long k;
double C12 = 0, C11 = 0, C22 = 0;
short* samples = Data;
for (k=0; k<NumBlocks*2; k+=2)
{
C12 += samples[k]*samples[k+1];
C11 += samples[k]*samples[k];
C22 += samples[k+1]*samples[k+1];
}
return C12/sqrt(C11*C22);
}
|