57 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
58 if(verbose>0) printf(
"%s()\n", __func__);
59 if(x==NULL || y==NULL || k==NULL || a==NULL || snr<1 || bnr<1) {
64 if(verbose>1) fprintf(stderr,
"invalid sample or BF number\n");
68 if(!(kmin>0.0) || kmin>=kmax) {
69 if(verbose>1) fprintf(stderr,
"invalid k range\n");
75 if(verbose>1) printf(
"computing k values\n");
78 a=log10(kmin); b=log10(kmax); c=(b-a)/(
double)(bnr-1);
79 if(verbose>3) printf(
" a := %g\n b := %g\n c := %g\n", a, b, c);
80 for(
int bi=0; bi<bnr; bi++) k[bi]=pow(10.0, (
double)bi*c+a);
84 int nnls_n, nnls_m, n, m, nnls_index[bnr];
85 double *nnls_a[bnr], *nnls_b, *nnls_zz, nnls_x[bnr], *nnls_mat,
86 nnls_wp[bnr], *dptr, nnls_rnorm;
87 if(verbose>1) printf(
"allocating memory for NNLS\n");
88 nnls_n=bnr; nnls_m=snr;
89 nnls_mat=(
double*)malloc(((nnls_n+2)*nnls_m)*
sizeof(
double));
91 if(verbose>1) fprintf(stderr,
"Error: cannot allocate memory for NNLS.\n");
95 for(n=0, dptr=nnls_mat; n<nnls_n; n++) {nnls_a[n]=dptr; dptr+=nnls_m;}
96 nnls_b=dptr; dptr+=nnls_m; nnls_zz=dptr;
98 if(verbose>1) printf(
"filling NNLS data matrix\n");
100 for(m=0; m<nnls_m; m++) nnls_b[m]=y[m];
103 for(n=0; n<nnls_n; n++)
104 for(m=0; m<nnls_m; m++)
105 nnls_a[n][m]=exp(-k[n]*x[m]);
108 if(w!=NULL)
nnlsWght(nnls_n, nnls_m, nnls_a, nnls_b, w);
111 if(verbose>1) printf(
"applying NNLS\n");
112 ret=
nnls(nnls_a, nnls_m, nnls_n, nnls_b, nnls_x, &nnls_rnorm, nnls_wp, nnls_zz, nnls_index);
114 if(verbose>1) fprintf(stderr,
"NNLS solution not possible.\n");
120 if(a!=NULL)
for(n=0; n<nnls_n; n++) a[n]=nnls_x[n];
124 if(verbose>1) printf(
"computing yfit[]\n");
125 for(m=0; m<nnls_m; m++) {
127 for(n=0; n<nnls_n; n++)
129 yfit[m]+=nnls_x[n]*exp(-k[n]*x[m]);
161 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
162 if(verbose>0) printf(
"%s(k[], a[], %d, *kmin, *kmax)\n", __func__, n);
163 if(k==NULL || a==NULL || n<1) {
167 double max=nan(
""), min=nan(
"");
168 for(
int i=0; i<n; i++)
if(a[i]>0.0 && isfinite(k[i])) {
169 if(isnan(max) || k[i]>max) max=k[i];
170 if(isnan(min) || k[i]<min) min=k[i];
173 printf(
" kmin := %g\n", min);
174 printf(
" kmax := %g\n", max);
176 if(kmin!=NULL) *kmin=min;
177 if(kmax!=NULL) *kmax=max;
178 if(isnan(min) || isnan(max)) {
243 if(k==NULL || a==NULL || n<1 || ke==NULL || ae==NULL || ne<1)
return(0);
246 double *buf=NULL, *ok, *oa;
250 buf=(
double*)malloc(2*bfn*
sizeof(
double));
if(buf==NULL)
return(0);
260 ii+=i;
if(n-ii<1)
break;
263 oa[nn]=a[ii]; ok[nn]=k[ii];
273 }
while(ii<n && nn<bfn);
275 printf(
"\n\tCluster a and k values\n");
276 for(
int i=0; i<bfn; i++) printf(
"\t%g\t%g\n", oa[i], ok[i]);
277 printf(
"\n"); fflush(stdout);
282 for(
int i=bfn; i<ne; i++) ke[i]=ae[i]=0.0;
283 if(buf!=NULL) free(buf);
290 if(0) printf(
"smallest |a[%d]|=%g\n", ami, oa[ami]);
294 else if(ami==bfn-1) ci=bfn-2;
295 else {
if(fabs(oa[ami-1])<fabs(oa[ami+1])) ci=ami-1;
else ci=ami+1;}
296 if(0) printf(
"combining clusters %d and %d\n", 1+ami, 1+ci);
298 ok[ci]=(oa[ami]*ok[ami]+oa[ci]*ok[ci])/(oa[ami]+oa[ci]);
301 for(
int i=ami+1; i<bfn; i++) oa[i-1]=oa[i];
302 for(
int i=ami+1; i<bfn; i++) ok[i-1]=ok[i];
305 printf(
"\n\tAfter reduction, cluster a and k values\n");
306 for(
int i=0; i<bfn; i++) printf(
"\t%g\t%g\n", oa[i], ok[i]);
307 printf(
"\n"); fflush(stdout);
310 for(
int i=0; i<ne; i++) ae[i]=oa[i];
311 for(
int i=0; i<ne; i++) ke[i]=ok[i];
312 if(buf!=NULL) free(buf);
int spectralDExp(const double *x, const double *y, double *w, const int snr, const double kmin, const double kmax, const int bnr, double *k, double *a, double *yfit, TPCSTATUS *status)
int nnls(double **a, int m, int n, double *b, double *x, double *rnorm, double *wp, double *zzp, int *indexp)