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
|
#include "ltfat.h"
#include "ltfat_types.h"
#include "reassign_typeconstant.h"
LTFAT_EXTERN void
LTFAT_NAME(gabreassign)(const LTFAT_REAL *s, const LTFAT_REAL *tgrad,
const LTFAT_REAL *fgrad, const ltfatInt L, const ltfatInt W,
const ltfatInt a, const ltfatInt M, LTFAT_REAL *sr)
{
ltfatInt ii, posi, posj;
const ltfatInt N = L / a;
const ltfatInt b = L / M;
ltfatInt *timepos = ltfat_malloc(N * sizeof * timepos);
ltfatInt *freqpos = ltfat_malloc(M * sizeof * freqpos);
fftindex(N, timepos);
fftindex(M, freqpos);
/* Zero the output array. */
memset(sr, 0, M * N * W * sizeof * sr);
for (ltfatInt w = 0; w < W; w++)
{
for (ii = 0; ii < M; ii++)
{
for (ltfatInt jj = 0; jj < N; jj++)
{
/* Do a 'round' followed by a 'mod'. 'round' is not
* present in all libraries, so use trunc(x+.5) instead */
/*posi=positiverem((ltfatInt)trunc(tgrad[ii+jj*M]/b+freqpos[ii]+.5),M);
posj=positiverem((ltfatInt)trunc(fgrad[ii+jj*M]/a+timepos[jj]+.5),N);*/
posi = positiverem(ltfat_round(tgrad[ii + jj * M] / b + freqpos[ii]), M);
posj = positiverem(ltfat_round(fgrad[ii + jj * M] / a + timepos[jj]), N);
sr[posi + posj * M] += s[ii + jj * M];
}
}
}
LTFAT_SAFEFREEALL(freqpos, timepos);
}
LTFAT_EXTERN void
LTFAT_NAME(filterbankphasegrad)(const LTFAT_COMPLEX* c [],
const LTFAT_COMPLEX* ch[],
const LTFAT_COMPLEX* cd[],
const ltfatInt M,
const ltfatInt N[],
const ltfatInt L,
const LTFAT_REAL minlvl,
LTFAT_REAL* tgrad[],
LTFAT_REAL* fgrad[],
LTFAT_REAL* cs[])
{
#define FOREACHCOEF \
for(ltfatInt m=0;m<M;++m){\
for(ltfatInt ii=0;ii<N[m];++ii){
#define ARRAYEL(c) ((c)[m][ii])
#define ENDFOREACHCOEF }}
LTFAT_REAL minlvlAlt = LTFAT_COMPLEXH(cabs)(c[0][0]);
// Compute spectrogram from coefficients
// Keep max value
FOREACHCOEF
LTFAT_REAL en = LTFAT_COMPLEXH(cabs)(ARRAYEL(c))*LTFAT_COMPLEXH(cabs)(ARRAYEL(c));
ARRAYEL(cs) = en;
if(en>minlvlAlt)
minlvlAlt = en;
ENDFOREACHCOEF
// Adjust minlvl
minlvlAlt *= minlvl;
// Force spectrogram values less tha minLvlAlt to minlvlAlt
FOREACHCOEF
LTFAT_REAL csEl = ARRAYEL(cs);
if(csEl<minlvlAlt)
ARRAYEL(cs) = minlvlAlt;
ENDFOREACHCOEF
// Instantaneous frequency
FOREACHCOEF
LTFAT_REAL tgradEl = LTFAT_COMPLEXH(creal)(
ARRAYEL(cd)*LTFAT_COMPLEXH(conj)(ARRAYEL(c))/ARRAYEL(cs)
)/L*2;
ARRAYEL(tgrad) = fabs(tgradEl)<=2?tgradEl:0.0f;
ENDFOREACHCOEF
FOREACHCOEF
ARRAYEL(fgrad) = LTFAT_COMPLEXH(cimag)(
ARRAYEL(ch)*LTFAT_COMPLEXH(conj)(ARRAYEL(c))/ARRAYEL(cs)
);
ENDFOREACHCOEF
#undef FOREACHCOEF
#undef ENDFOREACHCOEF
#undef ARRAYEL
}
LTFAT_EXTERN void
LTFAT_NAME(filterbankreassign)(const LTFAT_REAL *s[],
const LTFAT_REAL *tgrad[],
const LTFAT_REAL *fgrad[],
const ltfatInt N[], const double a[],
const double cfreq[], const ltfatInt M,
LTFAT_REAL *sr[],
fbreassHints hints,
fbreassOptOut *repos)
{
#define CHECKZEROCROSSINGANDBREAK( CMP, SIGN) \
{ \
if ( (tmptgrad) CMP 0.0 )\
{\
if (fabs(tmptgrad) < fabs(oldtgrad))\
{\
tgradIdx[jj] = ii;\
}\
else\
{\
tgradIdx[jj] = ii SIGN 1;\
}\
break;\
}\
oldtgrad = tmptgrad;\
}
ltfatInt* chan_pos = NULL;
int doTimeWraparound = !(hints & REASS_NOTIMEWRAPAROUND);
if (repos)
{
chan_pos = ltfat_malloc((M + 1) * sizeof * chan_pos);
chan_pos[0] = 0.0;
for (ltfatInt ii = 0; ii < M; ii++)
{
chan_pos[ii + 1] = chan_pos[ii] + N[ii];
}
}
/* Limit tgrad? */
LTFAT_REAL oneover2 = 1.0 / 2.0;
// This will hold center frequencies modulo 2.0
double *cfreq2 = ltfat_malloc(M * sizeof * cfreq2);
for (ltfatInt m = 0; m < M; m++)
{
// Zero the output arrays
memset(sr[m], 0, N[m]*sizeof(LTFAT_REAL));
// This is effectivelly modulo by 2.0
cfreq2[m] = cfreq[m] - floor(cfreq[m] * oneover2) * 2.0;
}
ltfatInt* tgradIdx = NULL;
ltfatInt* fgradIdx = NULL;
ltfatInt Nold = 0;
for (ltfatInt m = M - 1; m >= 0; m--)
{
// Ensure the temporary arrays have proper lengths
if (N[m] > Nold)
{
if (tgradIdx)
{
ltfat_free(tgradIdx);
}
if (fgradIdx)
{
ltfat_free(fgradIdx);
}
tgradIdx = ltfat_malloc(N[m] * sizeof * tgradIdx);
fgradIdx = ltfat_malloc(N[m] * sizeof * fgradIdx);
Nold = N[m];
}
// We will use this repeatedly
LTFAT_REAL cfreqm = cfreq2[m];
/************************
*
* Calculating frequency reassignment
*
* **********************
*/
for (ltfatInt jj = 0; jj < N[m]; jj++)
{
//
LTFAT_REAL tmptgrad = 0.0;
LTFAT_REAL tgradmjj = tgrad[m][jj] + cfreqm;
LTFAT_REAL oldtgrad = 10; // 10 seems to be big enough
// Zero this in case it falls trough, although it might not happen
tgradIdx[jj] = 0;
if (tgrad[m][jj] > 0)
{
ltfatInt ii;
// Search for zero crossing
// If the gradient is bigger than 0, start from m upward....
for (ii = m; ii < M; ii++)
{
tmptgrad = cfreq2[ii] - tgradmjj;
CHECKZEROCROSSINGANDBREAK( >= , -)
}
// If the previous for does not break, ii == M
if (ii == M && tmptgrad < 0.0)
{
for (ltfatInt ii = 0; ii < m ; ii++)
{
tmptgrad = cfreq2[ii] - tgradmjj + 2.0;
CHECKZEROCROSSINGANDBREAK( >= , -)
}
}
if (tgradIdx[jj] < 0)
{
tgradIdx[jj] = M - 1;
}
}
else
{
ltfatInt ii;
for (ii = m; ii >= 0; ii--)
{
tmptgrad = cfreq2[ii] - tgradmjj;
CHECKZEROCROSSINGANDBREAK( <= , +)
}
// If the previous for does not break, ii=-1
if (ii == -1 && tmptgrad > 0.0)
{
for (ltfatInt ii = M - 1; ii >= m; ii--)
{
tmptgrad = cfreq2[ii] - tgradmjj - 2.0;
CHECKZEROCROSSINGANDBREAK( <= , +)
}
}
if (tgradIdx[jj] >= M)
{
tgradIdx[jj] = 0;
}
}
}
/**********************************
* *
* Calculating time-reassignment *
* *
**********************************/
for (ltfatInt jj = 0; jj < N[m]; jj++)
{
ltfatInt tmpIdx = tgradIdx[jj];
ltfatInt fgradIdxTmp = ltfat_round( (fgrad[m][jj] + a[m] * jj) / a[tmpIdx]);
if(doTimeWraparound)
{
fgradIdx[jj] = positiverem( fgradIdxTmp, N[tmpIdx]);
}
else
{
fgradIdx[jj] = rangelimit( fgradIdxTmp, 0, N[tmpIdx]-1);
}
}
for (ltfatInt jj = 0; jj < N[m]; jj++)
{
sr[tgradIdx[jj]][fgradIdx[jj]] += s[m][jj];
}
if (repos && chan_pos)
{
for (ltfatInt jj = 0; jj < N[m]; jj++)
{
ltfatInt tmpIdx = chan_pos[tgradIdx[jj]] + fgradIdx[jj] ;
ltfatInt* tmpl = &repos->reposl[tmpIdx];
repos->repos[tmpIdx][*tmpl] = chan_pos[m] + jj;
(*tmpl)++;
if (*tmpl >= repos->reposlmax[tmpIdx])
{
fbreassOptOut_expand(repos, tmpIdx);
}
}
}
}
LTFAT_SAFEFREEALL(tgradIdx, fgradIdx, cfreq2, chan_pos);
#undef CHECKZEROCROSSINGANDBREAK
}
|