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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
/****************************************************************************
DRC: Digital Room Correction
Copyright (C) 2002-2004 Denis Sbragion
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
You can contact the author on Internet at the following address:
d.sbragion@infotecna.it
This program uses the parsecfg library from Yuuki NINOMIYA. De
tails on this library can be found in the parsecfg.c and par
secfg.h files. Many thanks to Yuuki NINOMIYA for this useful li
brary.
This program uses also the FFT routines from Takuya Ooura and
the GNU Scientific Library (GSL). Many thanks to Takuya Ooura
and the GSL developers for these efficient routines.
****************************************************************************/
/* Log sweep and inverse filter convolution program */
/* Includes */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "fftsg_h.h"
/* Output stringhe con sync output e parametro */
int sputsp(const char * s, const char * p)
{
int Res;
if (p == NULL)
Res = puts(s);
else
Res = printf("%s%s\n",s,p);
fflush(stdout);
return(Res);
}
/* Output stringhe con sync output */
int sputs(const char * s)
{
return(sputsp(s, NULL));
}
/* Determina la lunghezza di un file */
size_t FSize(char * FName)
{
long FS;
FILE * F;
if ((F = fopen(FName,"rb")) == NULL)
{
perror("Unable to open file");
return 0;
}
fseek(F,0,SEEK_END);
FS = ftell(F);
fclose(F);
return (size_t) FS;
}
/* Calola il valore RMS del segnale Sig */
DLReal GetRMSLevel(const DLReal * Sig,const int SigLen)
{
DLReal RMS;
int I;
RMS = 0;
for (I = 0; I < SigLen; I++)
RMS += Sig[I] * Sig[I];
return (DLReal) sqrt(RMS);
}
/* Halfcomplex array convolution */
void hcconvolve(DLReal * A,const DLReal * B,const int N)
{
int R;
int I;
DLReal T1;
DLReal T2;
A[0] *= B[0];
A[1] *= B[1];
for (R = 2,I = 3;R < N;R += 2,I += 2)
{
T1 = A[R] * B[R];
T2 = A[I] * B[I];
A[I] = ((A[R] + A[I]) * (B[R] + B[I])) - (T1 + T2);
A[R] = T1 - T2;
}
}
/* Main procedure */
int main(int argc, char * argv[])
{
/* Input parameters */
char * SweepFile;
char * InverseFile;
char * OutFile;
char * RefSweepFile;
DLReal MinGain;
DLReal DLStart;
/* Convolution parameters */
int SS;
int IS;
int RS;
int CS;
int CL;
int I;
int J;
DLReal * Sweep;
DLReal * Inverse;
float RF;
/* Dip limiting */
DLReal RMSLevel;
DLReal RMSMin;
DLReal DLSLevel;
DLReal DLLevel;
DLReal DLGFactor;
DLReal DLAbs;
DLReal DLTheta;
/* Peak position */
int MP;
DLReal AMax;
/* Input/output file */
FILE * IOF;
/* Initial message */
sputs("\nLSConv 1.1.0: log sweep and inverse filter convolution.");
sputs("Copyright (C) 2002-2011 Denis Sbragion");
#ifdef UseDouble
sputs("\nCompiled with double precision arithmetic.");
#else
sputs("\nCompiled with single precision arithmetic.");
#endif
sputs("\nThis program may be freely redistributed under the terms of");
sputs("the GNU GPL and is provided to you as is, without any warranty");
sputs("of any kind. Please read the file \"COPYING\" for details.");
/* Check program arguments */
if (argc < 4 || (argc > 4 && argc < 6))
{
sputs("\nUsage: LSConv sweepfile inversefile outfile [refsweep mingain [dlstart]]");
sputs("\nParameters:\n");
sputs(" sweepfile: sweep file name");
sputs(" inversefile: inverse sweep file name");
sputs(" outfile: output impulse response file");
sputs(" refsweep: reference channel sweep file name");
sputs(" mingain: min gain for reference channel inversion");
sputs(" dlstart: dip limiting start for reference channel inversion");
sputs("\nExample: lsconv sweep.pcm inverse.pcm impulse.pcm refchannel.pcm 0.1 0.8\n");
return 0;
}
/* Get the input parameters from the command line */
sputs("\nCommand line parsing.");
SweepFile = argv[1];
InverseFile = argv[2];
OutFile = argv[3];
if (argc > 4)
{
RefSweepFile = argv[4];
MinGain = (DLReal) atof(argv[5]);
}
else
RefSweepFile = NULL;
if (argc >= 7)
DLStart = (DLReal) atof(argv[6]);
else
DLStart = (DLReal) 2.0;
/* Computes the convolution length */
sputs("Convolution length computation.");
SS = FSize(SweepFile) / sizeof(float);
IS = FSize(InverseFile) / sizeof(float);
if (RefSweepFile != NULL)
RS = FSize(RefSweepFile) / sizeof(float);
else
RS = 1;
CL = SS + IS + RS - 2;
for(CS = 1;CS < CL;CS <<= 1);
/* Convolution arrays allocation */
sputs("Convolution arrays allocation.");
if ((Sweep = (DLReal *) malloc(sizeof(DLReal) * CS)) == NULL)
{
sputs("Memory allocation failure.");
return 1;
}
if ((Inverse = (DLReal *) malloc(sizeof(DLReal) * CS)) == NULL)
{
sputs("Memory allocation failure.");
free(Sweep);
return 1;
}
/* Read the inverse file */
sputsp("Reading inverse file: ",InverseFile);
if ((IOF = fopen(InverseFile,"rb")) == NULL)
{
perror("Unable to open inverse file");
return 1;
}
for(I = 0;I < IS;I++)
{
fread(&RF,sizeof(float),1,IOF);
Inverse[I] = (DLReal) RF;
}
for (I = IS;I < CS;I++)
Inverse[I] = (DLReal) 0.0;
fclose(IOF);
sputs("Inverse filter FFT...");
rdft(CS,OouraRForward,Inverse);
/* Check the reference file */
if (RefSweepFile != NULL)
{
/* Read the reference file */
sputsp("Reading reference file: ",RefSweepFile);
if ((IOF = fopen(RefSweepFile,"rb")) == NULL)
{
perror("Unable to open reference file");
return 1;
}
for (I = 0;I < CS;I++)
Sweep[I] = (DLReal) 0.0;
for(I = CS - IS,J = 0;J < RS;I++,J++)
{
fread(&RF,sizeof(float),1,IOF);
Sweep[I % CS] = (DLReal) RF;
}
fclose(IOF);
/* Convolving sweep and inverse */
sputs("Reference inversion and convolution...");
rdft(CS,OouraRForward,Sweep);
hcconvolve(Sweep,Inverse,CS);
/* Computes the RMS Level */
RMSLevel = Sweep[0] * Sweep[0];
for (I = 2,J = 3;I < CS;I += 2,J += 2)
RMSLevel += Sweep[I] * Sweep[I] + Sweep[J] * Sweep[J];
RMSLevel = (DLReal) sqrt(2.0 * RMSLevel / CS);
RMSMin = MinGain * RMSLevel;
/* Check the gain truncation type */
if (DLStart >= (DLReal) 1.0)
{
/* Check starting components */
if (Sweep[0] < RMSMin)
Sweep[0] = ((DLReal) 1.0) / RMSMin;
else
Sweep[0] = ((DLReal) 1.0) / Sweep[0];
if (Sweep[1] < RMSMin)
Sweep[1] = ((DLReal) 1.0) / RMSMin;
else
Sweep[1] = ((DLReal) 1.0) / Sweep[1];
/* Gain truncation scan */
for (I = 2,J = 3;I < CS;I += 2,J += 2)
{
/* Gain computation */
DLAbs = (DLReal) hypot(Sweep[I],Sweep[J]);
/* Gain check and limitation */
if (DLAbs < RMSMin)
{
/* Gain limited inversion */
DLTheta = (DLReal) atan2(Sweep[J],Sweep[I]);
DLAbs = ((DLReal) 1.0) / RMSMin;
Sweep[I] = DLAbs * (DLReal) cos(-DLTheta);
Sweep[J] = DLAbs * (DLReal) sin(-DLTheta);
}
else
{
/* Inversion and renormalization */
DLAbs = ((DLReal) 1.0) / (DLAbs * DLAbs);
Sweep[I] *= DLAbs;
Sweep[J] *= -DLAbs;
}
}
}
else
{
/* Determina i fattori per la limitazione guadagno */
DLSLevel = RMSMin / DLStart;
DLGFactor = DLSLevel - RMSMin;
/* Check starting components */
if (Sweep[0] < DLSLevel)
{
/* Riassegna il guadagno del filtro */
DLLevel = (DLSLevel - Sweep[0]) / DLGFactor;
DLLevel = DLLevel / (((DLReal) 1.0) + DLLevel);
Sweep[0] = ((DLReal) 1.0) / (DLSLevel - DLGFactor * DLLevel);
}
else
Sweep[0] = ((DLReal) 1.0) / Sweep[0];
if (Sweep[1] < DLSLevel)
{
/* Riassegna il guadagno del filtro */
DLLevel = (DLSLevel - Sweep[1]) / DLGFactor;
DLLevel = DLLevel / (((DLReal) 1.0) + DLLevel);
Sweep[1] = ((DLReal) 1.0) / (DLSLevel - DLGFactor * DLLevel);
}
else
Sweep[1] = ((DLReal) 1.0) / Sweep[1];
/* Scansione per limitazione guadagno */
for (I = 2,J = 3;I < CS;I += 2,J += 2)
{
DLAbs = (DLReal) hypot(Sweep[I],Sweep[J]);
if (DLAbs <= (DLReal) 0.0)
{
Sweep[I] = ((DLReal) 1.0) / RMSMin;
Sweep[J] = 0;
}
else
if (DLAbs < DLSLevel)
{
/* Riassegna il guadagno del filtro */
DLLevel = (DLSLevel - DLAbs) / DLGFactor;
DLLevel = DLLevel / (((DLReal) 1.0) + DLLevel);
DLAbs = ((DLReal) 1.0) / (DLSLevel - DLGFactor * DLLevel);
/* Gain limited inversion */
DLTheta = (DLReal) atan2(Sweep[J],Sweep[I]);
Sweep[I] = DLAbs * (DLReal) cos(-DLTheta);
Sweep[J] = DLAbs * (DLReal) sin(-DLTheta);
}
else
{
/* Inversion and renormalization */
DLAbs = ((DLReal) 1.0) / (DLAbs * DLAbs);
Sweep[I] *= DLAbs;
Sweep[J] *= -DLAbs;
}
}
}
/* Inverse convolution */
hcconvolve(Inverse,Sweep,CS);
}
/* Read the sweep file */
sputsp("Reading sweep file: ",SweepFile);
if ((IOF = fopen(SweepFile,"rb")) == NULL)
{
perror("Unable to open sweep file");
return 1;
}
for(I = 0;I < SS;I++)
{
fread(&RF,sizeof(float),1,IOF);
Sweep[I] = (DLReal) RF;
}
fclose(IOF);
for (I = SS;I < CS;I++)
Sweep[I] = (DLReal) 0.0;
/* Convolving sweep and inverse */
sputs("Sweep and inverse convolution...");
rdft(CS,OouraRForward,Sweep);
hcconvolve(Sweep,Inverse,CS);
/* Impulse response recover */
rdft(CS,OouraRBackward,Sweep);
for (I = 0;I < CS;I++)
Sweep[I] *= (DLReal) (2.0 / CS);
/* Peak search */
sputs("Finding impulse response peak value...");
MP = 0;
AMax = (DLReal) 0.0;
for (I = 0;I < CS;I++)
if ((DLReal) fabs(Sweep[I]) > AMax)
{
MP = I;
AMax = (DLReal) fabs(Sweep[I]);
}
printf("Peak position: %d\n",MP);
if (AMax > (DLReal) 0.0)
printf("Peak value: %f (%f dB)\n",(double) AMax, (double) (20 * log10((double) AMax)));
else
printf("Peak value: %f (-inf dB)\n",(double) AMax);
fflush(stdout);
/* Writes output file */
sputsp("Writing output file: ",OutFile);
if ((IOF = fopen(OutFile,"wb")) == NULL)
{
perror("Unable to open output file");
return 1;
}
for(I = 0;I < CL;I++)
{
RF = (float) Sweep[I];
fwrite(&RF,sizeof(float),1,IOF);
}
fclose(IOF);
/* Memory deallocation */
free(Sweep);
free(Inverse);
/* Execution completed */
sputs("Completed.");
return 0;
}
|