📄 simquan.c
字号:
/*
* SIMQUAN A SIMULINK quantizer block
*
* Syntax: [sys, x0] = regdown(t, x, u, flag, k, partition, codebook)
* This block quantizes individual element of the input vector using
* scalar quantization method. The input vector have dimention n, the
* partition is a N-1 vector, codebook is a length N vector. The output
* of this block is a 2N+1 vector with the first n elements being the
* index, which is one of the N integers in the range [0 : N-1]. The second
* N elements are the quantized output. The last N elements of the output
* vector is the estimation of the distortion.
*
* Wes Wang 10/6, 1994
*
* Copyright (c) 1994-96 by The MathWorks, Inc.
* All Rights Reserved
* $Revision: 1.1 $ $Date: 1996/04/01 19:04:36 $
*/
#define S_FUNCTION_NAME simquan
#ifdef MATLAB_MEX_FILE
#include <stdio.h> /* needed for declaration of sprintf */
#include "mex.h" /* needed for declaration of mexErrMsgTxt */
#endif
/*
* need to include simstruc.h for the definition of the SimStruct and
* its associated macro definitions.
*/
#include "simstruc.h"
/*
* Defines for easy access of the input parameters
*/
#define NUM_ARGS 3
#define NUM_INPUT ssGetArg(S,0)
#define PARTITION ssGetArg(S,1)
#define CODEBOOK ssGetArg(S,2)
/*
* mdlInitializeSizes - called to initialize the sizes array stored in
* the SimStruct. The sizes array defines the
* characteristics (number of inputs, outputs,
* states, etc.) of the S-Function.
*/
static void mdlInitializeSizes(S)
SimStruct *S;
{
int Num_Index;
int Num_Input = (int)mxGetPr(NUM_INPUT)[0];
/*
* Set-up size information.
*/
if (ssGetNumArgs(S) == NUM_ARGS) {
/* check the dimensions */
if ((mxGetN(NUM_INPUT) != 1) || (mxGetM(NUM_INPUT) != 1)) {
#ifdef MATLAB_MEX_FILE
mexErrMsgTxt("Input size must be a nonempty scalar.");
#endif
}
Num_Index = mxGetN(CODEBOOK) * mxGetM(CODEBOOK);
if (Num_Index - 1 != mxGetN(PARTITION) * mxGetM(PARTITION)) {
#ifdef MATLAB_MEX_FILE
mexErrMsgTxt("The vector size for PARTITION must be the size of CODEBOOK -1.");
#endif
}
if (Num_Index <= 1) {
#ifdef MATLAB_MEX_FILE
mexErrMsgTxt("The vector PARTITION cannot be empty.");
#endif
}
ssSetNumContStates( S, 0);
ssSetNumDiscStates( S, 0);
ssSetNumInputs( S, Num_Input+1);
ssSetNumOutputs( S, 3*Num_Input);
ssSetDirectFeedThrough(S, 1);
ssSetNumInputArgs( S, NUM_ARGS);
ssSetNumSampleTimes( S, 1);
/* Real number for distortion computation, first number is the number of
* elements have been processed */
ssSetNumRWork( S, 1 + Num_Input);
ssSetNumIWork( S, 1);
ssSetNumPWork( S, 0);
} else {
#ifdef MATLAB_MEX_FILE
char err_msg[256];
sprintf(err_msg, "Wrong number of input arguments passed to S-function MEX-file.\n"
"%d input arguments were passed in when expecting %d input arguments.\n", ssGetNumArgs(S) + 4, NUM_ARGS + 4);
mexErrMsgTxt(err_msg);
#endif
}
}
/*
* mdlInitializeSampleTimes - initializes the array of sample times stored in
* the SimStruct associated with this S-Function.
*/
static void mdlInitializeSampleTimes(S)
SimStruct *S;
{
/*
* Note, blocks that are continuous in nature should have a single
* sample time of 0.0.
*/
ssSetSampleTimeEvent(S, 0, 0.0);
ssSetOffsetTimeEvent(S, 0, 0.0);
}
/*
* mdlInitializeConditions - initializes the states for the S-Function
*/
static void mdlInitializeConditions(x0, S)
double *x0;
SimStruct *S;
{
int Num_Input = ssGetNumInputs(S) - 1;
int Num_Index = mxGetN(PARTITION) * mxGetM(PARTITION);
int *J_Bound = ssGetIWork(S);
double *Num_Done = ssGetRWork(S);
double *Distortion = ssGetRWork(S) + 1;
int i;
/*
* Initialize the buffer to all zeros, we could allow this to
* be an additional paramter.
*/
for (i = 0; i < Num_Input; i++)
*Distortion++ = 0.;
*Num_Done = 0.;
*J_Bound = Num_Index;
}
/*
* mdlOutputs - computes the outputs of the S-Function
*/
static void mdlOutputs(y, x, u, S, tid)
double *y, *x, *u;
SimStruct *S;
int tid;
{
int Num_Input = ssGetNumInputs(S) - 1;
if (u[Num_Input]) {
int *J_Bound = ssGetIWork(S);
double *Num_Done = ssGetRWork(S);
double *Distortion = ssGetRWork(S) + 1;
int i, j;
double Num_Done_1, Dist_tmp;
Num_Done_1 = *Num_Done;
*Num_Done = *Num_Done + 1;
for (i = 0; i < Num_Input; i++) {
/* for each input */
j = 0;
while ((u[i] > mxGetPr(PARTITION)[j]) && (j < *J_Bound))
j++;
/* index */
y[i] = j;
/* Quantized value */
y[i + Num_Input] = mxGetPr(CODEBOOK)[j];
/* Distortion computation */
Dist_tmp = (u[i] - mxGetPr(CODEBOOK)[j]);
Dist_tmp *= Dist_tmp;
if (Num_Done_1) {
Dist_tmp = (Dist_tmp + Distortion[i] * Num_Done_1) / *Num_Done;
}
Distortion[i] = Dist_tmp;
y[i + Num_Input + Num_Input] = Dist_tmp;
}
}
}
/*
* mdlUpdate - computes the discrete states of the S-Function
*/
static void mdlUpdate(x, u, S, tid)
double *x, *u;
SimStruct *S;
int tid;
{
}
/*
* mdlDerivatives - computes the derivatives of the S-Function
*/
static void mdlDerivatives(dx, x, u, S, tid)
double *dx, *x, *u;
SimStruct *S;
int tid;
{
}
/*
* mdlTerminate - called at termination of model execution.
*/
static void mdlTerminate(S)
SimStruct *S;
{
}
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-File interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -