📄 step.c
字号:
/*****************************************************************************/
/* Author: Brendt Wohlberg (Los Alamos National Laboratory). */
/* Copyright 2001 University of California. */
/*****************************************************************************/
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <math.h>
#include "Extension.h"
#include "Step.h"
int limited_step_convolve_float(const float* filter_taps, int filter_length,
const float* input, int input_length,
float* output) {
int k, l;
if (input_length < filter_length || !filter_taps || !input || !output)
return 0;
for (k = 0; k < input_length - filter_length + 1; k++) {
output[k] = 0;
for (l = 0; l < filter_length; l++)
output[k] += input[k+l]*filter_taps[filter_length-1-l];
}
return 1;
}
int compute_step_float(const ExtensionDesc* ext, const float* filter_taps,
int filter_offset, int filter_length,
const float* unmod_channel, int unmod_channel_offset,
int unmod_channel_length, float* step_output_branch,
int mod_channel_offset, int mod_channel_length) {
int unmod_channel_required_offset, unmod_channel_required_length;
int extleft, extrght;
float* unmod_channel_extended;
unmod_channel_required_offset = mod_channel_offset - filter_offset -
filter_length + 1;
unmod_channel_required_length = mod_channel_length + filter_length - 1;
unmod_channel_extended = malloc(sizeof(float)*unmod_channel_required_length);
if (!unmod_channel_extended)
return 0;
extleft = unmod_channel_offset - unmod_channel_required_offset;
extrght = unmod_channel_required_length - unmod_channel_length - extleft;
if (!extend_channel_float(ext, extleft, extrght, unmod_channel,
unmod_channel_length, unmod_channel_extended)) {
free(unmod_channel_extended);
return 0;
}
if (!limited_step_convolve_float(filter_taps, filter_length,
unmod_channel_extended,
unmod_channel_required_length,
step_output_branch)) {
free(unmod_channel_extended);
return 0;
}
free(unmod_channel_extended);
return 1;
}
int do_step_float(const ExtensionDesc* ext, const float* filter_taps,
int filter_offset, int filter_length,
const float* unmod_channel, int unmod_channel_offset,
int unmod_channel_length, float* mod_channel,
int mod_channel_offset, int mod_channel_length) {
float* step_output_branch;
int k;
step_output_branch = malloc(sizeof(float)*mod_channel_length);
if (!step_output_branch)
return 0;
if (!compute_step_float(ext, filter_taps, filter_offset, filter_length,
unmod_channel, unmod_channel_offset,
unmod_channel_length, step_output_branch,
mod_channel_offset, mod_channel_length))
return 0;
for (k = 0; k < mod_channel_length; k++)
mod_channel[k] += step_output_branch[k];
free(step_output_branch);
return 1;
}
int undo_step_float(const ExtensionDesc* ext, const float* filter_taps,
int filter_offset, int filter_length,
const float* unmod_channel, int unmod_channel_offset,
int unmod_channel_length, float* mod_channel,
int mod_channel_offset, int mod_channel_length) {
float* step_output_branch;
int k;
step_output_branch = malloc(sizeof(float)*mod_channel_length);
if (!step_output_branch)
return 0;
if (!compute_step_float(ext, filter_taps, filter_offset, filter_length,
unmod_channel, unmod_channel_offset,
unmod_channel_length, step_output_branch,
mod_channel_offset, mod_channel_length))
return 0;
for (k = 0; k < mod_channel_length; k++)
mod_channel[k] -= step_output_branch[k];
free(step_output_branch);
return 1;
}
int limited_step_convolve_int(const int* filter_alpha, int filter_length,
int filter_epsilon, int rounding_beta,
const int* input, int input_length,
int* output) {
int sum, k, l;
if (input_length < filter_length || !filter_alpha || !input || !output)
return 0;
for (k = 0; k < input_length - filter_length + 1; k++) {
sum = 0;
for (l = 0; l < filter_length; l++)
sum += input[k+l]*filter_alpha[filter_length-1-l];
output[k] = floor(((float)sum + rounding_beta)/(1 << filter_epsilon));
}
return 1;
}
int compute_step_int(const ExtensionDesc* ext, const int* filter_taps,
int filter_offset, int filter_length,
int filter_epsilon, int rounding_beta,
const int* unmod_channel, int unmod_channel_offset,
int unmod_channel_length, int* step_output_branch,
int mod_channel_offset, int mod_channel_length) {
int unmod_channel_required_offset, unmod_channel_required_length;
int extleft, extrght;
int* unmod_channel_extended;
unmod_channel_required_offset = mod_channel_offset - filter_offset -
filter_length + 1;
unmod_channel_required_length = mod_channel_length + filter_length - 1;
unmod_channel_extended = malloc(sizeof(int)*unmod_channel_required_length);
if (!unmod_channel_extended)
return 0;
extleft = unmod_channel_offset - unmod_channel_required_offset;
extrght = unmod_channel_required_length - unmod_channel_length - extleft;
if (!extend_channel_int(ext, extleft, extrght, unmod_channel,
unmod_channel_length, unmod_channel_extended)) {
free(unmod_channel_extended);
return 0;
}
if (!limited_step_convolve_int(filter_taps, filter_length,
filter_epsilon, rounding_beta,
unmod_channel_extended,
unmod_channel_required_length,
step_output_branch)) {
free(unmod_channel_extended);
return 0;
}
free(unmod_channel_extended);
return 1;
}
int do_step_int(const ExtensionDesc* ext, const int* filter_taps,
int filter_offset, int filter_length,
int filter_epsilon, int rounding_beta,
const int* unmod_channel, int unmod_channel_offset,
int unmod_channel_length, int* mod_channel,
int mod_channel_offset, int mod_channel_length) {
int* step_output_branch;
int k;
step_output_branch = malloc(sizeof(int)*mod_channel_length);
if (!step_output_branch)
return 0;
if (!compute_step_int(ext, filter_taps, filter_offset, filter_length,
filter_epsilon, rounding_beta,
unmod_channel, unmod_channel_offset,
unmod_channel_length, step_output_branch,
mod_channel_offset, mod_channel_length))
return 0;
for (k = 0; k < mod_channel_length; k++)
mod_channel[k] += step_output_branch[k];
free(step_output_branch);
return 1;
}
int undo_step_int(const ExtensionDesc* ext, const int* filter_taps,
int filter_offset, int filter_length,
int filter_epsilon, int rounding_beta,
const int* unmod_channel, int unmod_channel_offset,
int unmod_channel_length, int* mod_channel,
int mod_channel_offset, int mod_channel_length) {
int* step_output_branch;
int k;
step_output_branch = malloc(sizeof(int)*mod_channel_length);
if (!step_output_branch)
return 0;
if (!compute_step_int(ext, filter_taps, filter_offset, filter_length,
filter_epsilon, rounding_beta,
unmod_channel, unmod_channel_offset,
unmod_channel_length, step_output_branch,
mod_channel_offset, mod_channel_length))
return 0;
for (k = 0; k < mod_channel_length; k++)
mod_channel[k] -= step_output_branch[k];
free(step_output_branch);
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -