⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gtr.c

📁 QccPack implementation in C
💻 C
📖 第 1 页 / 共 2 页
字号:
                                            codebook->num_codewords,                                            winner))            {              QccErrorAddMessage("(QccAVQgtrEncode): Error calling QccVQCodebookMoveCodewordToFront()");              goto QccAVQError;            }          channel->channel_symbols[current_vector] = winner;          if (QccAVQgtrSendSideInfo(sideinfo, NULL,                                    QCCAVQSIDEINFO_FLAG_NOUPDATE))            {              QccErrorAddMessage("(QccAVQgtrEncode): Error calling QccAVQgtrSendSideInfo()");              goto QccAVQError;            }        }    }    QccVQCodebookSetCodewordLengths(codebook);    return_value = 0;  goto QccAVQExit;   QccAVQError:  return_value = 1; QccAVQExit:  QccDatasetFree(&tmp_dataset);  QccVectorFree(cnt);  if (new_codeword_symbol != NULL)    QccAVQSideInfoSymbolFree(new_codeword_symbol);  return(return_value);}int QccAVQgtrDecode(QccDataset *dataset,                    QccVQCodebook *codebook,                    const QccChannel *channel,                    QccAVQSideInfo *sideinfo){  int return_value;  int vector_dimension, block_size;  int current_vector;  QccAVQSideInfoSymbol *current_symbol = NULL;    if ((dataset == NULL) || (codebook == NULL) || (channel == NULL) ||      (sideinfo == NULL))    return(0);  if (sideinfo->fileptr == NULL)    return(0);  if ((dataset->vectors == NULL) ||      (channel->channel_symbols == NULL))    return(0);    vector_dimension = dataset->vector_dimension;  if ((vector_dimension != codebook->codeword_dimension) ||      (vector_dimension != sideinfo->vector_dimension))    {      QccErrorAddMessage("(QccAVQgtrEncode): Codebook %s, dataset %s, and sideinfo %s have different vector dimensions",                         codebook->filename, dataset->filename, sideinfo->filename);      goto QccAVQError;    }    if ((current_symbol = QccAVQSideInfoSymbolAlloc(vector_dimension)) == NULL)    {      QccErrorAddMessage("(QccAVQgtrDecode): Error calling QccAVQSideInfoSymbolAlloc()");      goto QccAVQError;    }  block_size = QccDatasetGetBlockSize(dataset);    for (current_vector = 0; current_vector < block_size; current_vector++)    {      if (QccAVQSideInfoSymbolReadDesired(sideinfo,                                          QCCAVQSIDEINFO_FLAG,                                          current_symbol) == NULL)        {          QccErrorAddMessage("(QccAVQgtrDecode): Error calling QccAVQSideInfoSymbolReadDesired()");          goto QccAVQError;        }            if (current_symbol->flag == QCCAVQSIDEINFO_FLAG_UPDATE)        {          /*  Update codebook  */          if (QccAVQSideInfoSymbolReadDesired(sideinfo,                                              QCCAVQSIDEINFO_VECTOR,                                              current_symbol) == NULL)            {              QccErrorAddMessage("(QccAVQgtrDecode): Error calling QccAVQSideInfoSymbolReadDesired()");              goto QccAVQError;            }          if (codebook->num_codewords < sideinfo->max_codebook_size)            {              if (QccVQCodebookAddCodeword(codebook,                                           (QccVQCodeword)                                           current_symbol->vector))                {                  QccErrorAddMessage("(QccAVQgtrDecode): Error calling QccVQCodebookAddCodeword()");                  goto QccAVQError;                }            }          else            QccVectorCopy((QccVector)codebook->codewords                          [codebook->num_codewords - 1],                           current_symbol->vector, vector_dimension);          if (QccVQCodebookMoveCodewordToFront(codebook,                                                codebook->num_codewords - 1))            {              QccErrorAddMessage("(QccAVQgtrEncode): Error calling QccVQCodebookMoveCodewordToFront()");              goto QccAVQError;            }        }      else        {          /*  No codebook update  */          if (QccVQCodebookMoveCodewordToFront(codebook,                                               channel->channel_symbols                                               [current_vector]))            {              QccErrorAddMessage("(QccAVQgtrEncode): Error calling QccVQCodebookMoveCodewordToFront()");              goto QccAVQError;            }        }            QccVectorCopy(dataset->vectors[current_vector],                    (QccVector)codebook->codewords[0],                    vector_dimension);    }    return_value = 0;  goto QccAVQExit;   QccAVQError:  return_value = 1; QccAVQExit:  if (current_symbol != NULL)    QccAVQSideInfoSymbolFree(current_symbol);  return(return_value);}int QccAVQgtrCalcRate(QccChannel *channel,                       QccAVQSideInfo *sideinfo,                      FILE *outfile,                       double *rate,                       int verbose,                      int entropy_code_sideinfo){  int return_value;  int total_updated, num_input_vectors;  double channel_entropy;  double num_bits_channel, num_bits_sideinfo, num_bits_total;  double num_bits_sideinfo_updatevectors, num_bits_sideinfo_flags;  double total_rate, channel_rate, sideinfo_rate;  double sideinfo_updatevectors_rate, sideinfo_flags_rate;  int symbol, component;  int precision;  QccAVQSideInfoSymbol *sideinfo_symbol = NULL;  QccChannel sideinfo_channel;  double sideinfo_entropy, sideinfo_flags_entropy;  double prob1, prob2;  if ((outfile == NULL) ||      (channel == NULL) ||      (sideinfo == NULL))    return(0);    if ((sideinfo_symbol =        QccAVQSideInfoSymbolAlloc(sideinfo->vector_dimension)) == NULL)    {      QccErrorAddMessage("(QccAVQgtrCalcRate): Error calling QccAVQSideInfoSymbolAlloc()");      goto QccError;    }  QccChannelInitialize(&sideinfo_channel);  sideinfo_channel.channel_length = channel->channel_length *    sideinfo->vector_dimension;  sideinfo_channel.alphabet_size = sideinfo->codebook_coder.num_levels;  sideinfo_channel.access_block_size = QCCCHANNEL_ACCESSWHOLEFILE;  if (QccChannelAlloc(&sideinfo_channel))    {      QccErrorAddMessage("(QccAVQgtrCalcRate): Error calling QccChannelAlloc()");      goto QccError;    }  sideinfo_channel.channel_length = 0;    num_input_vectors = sideinfo->N;  total_updated = 0;  for (symbol = 0; symbol < num_input_vectors; symbol++)    {      if (QccAVQSideInfoSymbolReadNextFlag(sideinfo,                                           sideinfo_symbol) == NULL)        {          QccErrorAddMessage("(QccAVQgtrCalcRate): Error calling QccAVQSideInfoSymbolReadNextFlag()");          goto QccError;        }      if (sideinfo_symbol->flag == QCCAVQSIDEINFO_FLAG_UPDATE)        {          if (QccAVQSideInfoSymbolReadDesired(sideinfo,                                              QCCAVQSIDEINFO_VECTOR,                                              sideinfo_symbol) == NULL)            {              QccErrorAddMessage("(QccAVQgtrCalcRate): Error calling QccAVQSideInfoSymbolReadDesired()");              goto QccError;            }          for (component = 0; component < sideinfo->vector_dimension;               component++, sideinfo_channel.channel_length++)            sideinfo_channel.channel_symbols              [sideinfo_channel.channel_length] =               sideinfo_symbol->vector_indices[component];          total_updated++;        }    }  if (sideinfo->codebook_coder.type != QCCSQSCALARQUANTIZER_GENERAL)    {      if (QccChannelNormalize(&sideinfo_channel))        {          QccErrorAddMessage("(QccAVQgtrCalcRate): Error calling QccChannelNormalize()");          goto QccError;        }    }  if ((sideinfo_entropy = QccChannelEntropy(&sideinfo_channel, 1)) < 0)    {      QccErrorAddMessage("(QccAVQgtrCalcRate): Error calling QccChannelEntropy()");      goto QccError;    }  if (QccChannelRemoveNullSymbols(channel))    {      QccErrorAddMessage("(QccAVQgtrCalcRate): Error calling QccChannelRemoveNullSymbols()");      goto QccError;    }  if ((channel_entropy = QccChannelEntropy(channel, 1)) < 0)    {      QccErrorAddMessage("(QccAVQgtrCalcRate): Error calling QccChannelEntropy()");      goto QccError;    }    prob1 = (double)total_updated/num_input_vectors;  prob2 = 1.0 - prob1;  sideinfo_flags_entropy =    (-prob1*QccMathLog2(prob1) - prob2*QccMathLog2(prob2));  if (!entropy_code_sideinfo)    {      precision =         (int)(log((double)sideinfo->codebook_coder.num_levels)/log(2.0) + 0.5);      num_bits_sideinfo_flags = num_input_vectors;      num_bits_sideinfo_updatevectors =        total_updated * sideinfo->vector_dimension * precision;    }  else    {      num_bits_sideinfo_flags = num_input_vectors * sideinfo_flags_entropy;      num_bits_sideinfo_updatevectors =        total_updated * sideinfo->vector_dimension * sideinfo_entropy;    }  num_bits_channel = (num_input_vectors - total_updated) * channel_entropy;  num_bits_sideinfo =     num_bits_sideinfo_flags + num_bits_sideinfo_updatevectors;  num_bits_total = num_bits_channel + num_bits_sideinfo;  channel_rate = num_bits_channel/num_input_vectors/sideinfo->vector_dimension;  sideinfo_updatevectors_rate =    num_bits_sideinfo_updatevectors /     num_input_vectors/sideinfo->vector_dimension;  sideinfo_flags_rate =    num_bits_sideinfo_flags /     num_input_vectors/sideinfo->vector_dimension;  sideinfo_rate =     num_bits_sideinfo/num_input_vectors/sideinfo->vector_dimension;  total_rate =     num_bits_total/num_input_vectors/sideinfo->vector_dimension;    if (verbose >= 2)    {      fprintf(outfile,              "Rate of GTR with channel\n   %s\n",              channel->filename);      fprintf(outfile,              "     and side information\n   %s\n",              sideinfo->filename);      fprintf(outfile,              "---------------------------------------------------\n");      fprintf(outfile,              "  Rate for VQ channel: %f\n",              channel_rate);      fprintf(outfile,              "  Rate for side information:\t");      fprintf(outfile,              "%.2f%% of vectors updated\n",              QccMathPercent(total_updated, num_input_vectors));      fprintf(outfile,              "      Rate for flags:\t\t%f (%5.2f%% of total rate)\n",              sideinfo_flags_rate,              QccMathPercent(sideinfo_flags_rate, total_rate));      fprintf(outfile,              "      Rate for update vectors:\t%f (%5.2f%% of total rate)\n",              sideinfo_updatevectors_rate,              QccMathPercent(sideinfo_updatevectors_rate, total_rate));      fprintf(outfile,              "    Total side information:\t%f (%5.2f%% of total rate)\n",              sideinfo_rate,              QccMathPercent(sideinfo_rate, total_rate));      fprintf(outfile,              "---------------------------------------------------\n");      fprintf(outfile,              "  Overall rate:      %f bits per original source symbol\n",               total_rate);    }  else    if (verbose == 1)      fprintf(outfile,              "%f\n", total_rate);  if (rate != NULL)    *rate = total_rate;  return_value = 0;  goto QccReturn; QccError:  return_value = 1; QccReturn:  QccAVQSideInfoSymbolFree(sideinfo_symbol);  QccChannelFree(&sideinfo_channel);  return(return_value);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -