📄 gen_05.cc
字号:
// file: $isip/class/algo/Generator/gen_05.cc// version: $Id: gen_05.cc,v 1.18 2002/08/16 20:04:14 parihar Exp $//// isip include files//#include "Generator.h"// method: apply//// arguments:// Vector<AlgorithmData>& output: (output) output data// const Vector< CircularBuffer<AlgorithmData> >& input: (input) input data//// return: a boolean value indicating status//// this method calls the appropriate computation methods//boolean Generator::apply(Vector<AlgorithmData>& output_a, const Vector< CircularBuffer<AlgorithmData> >& input_a) { // determine the number of channels and force the output to be // that number // long len = channels_d; output_a.setLength(len); boolean res = true; // start the debugging output // displayStart(this); // loop over the channels and call the compute method // for (long c = 0; c < len; c++) { // display the channel number // displayChannel(c); // Use the dummy VectorFloat as input because // we do not need the input for this method. // VectorFloat temp_input; // cmode_d: FRAME_INTERNAL // if (cmode_d == FRAME_INTERNAL) { // call AlgorithmData::makeVector to force the output vector for // this channel to be a VectorFloat, use the dummy VectorFloat as // input because we do not need the input for this method. // res &= compute(output_a(c).makeVectorFloat(), temp_input, DEF_COEF_TYPE, c); } // cmode_d: ACCUMULATE // else if (cmode_d == ACCUMULATE) { // call AlgorithmData::makeVector to force the output vector for // this channel to be a VectorFloat, use the dummy VectorFloat as // input because we do not need the input for this method. // res &= compute(output_a(c).makeVectorFloat(), temp_input, DEF_COEF_TYPE, c); } // cmode_d: CROSS_FRAME // else { return Error::handle(name(), L"apply", ERR_UNSUPM, __FILE__, __LINE__); } // set the coefficient type for the output // output_a(c).setCoefType(DEF_COEF_TYPE); } // finish the debugging output // displayFinish(this); // exit gracefully // return res;}// method: compute//// arguments:// VectorFloat& output: (output) operand// const VectorFloat& input: (input) operand// AlgorithmData::COEF_TYPE coef_type: (input) coeff type of input signal// long channel_index: (input) the channel index of input signal//// return: a boolean value containing status//// this method applies the specified window on the input vector//boolean Generator::compute(VectorFloat& output_a, const VectorFloat& input_a, AlgorithmData::COEF_TYPE coef_type_a, long channel_index_a) { // declare local variables // boolean status = false; // check whether we need to initialize // if (!is_valid_d) { init(); } // algorithm: SINE // if (algorithm_d == SINE) { status = computeSine(output_a, channel_index_a); } // algorithm: GAUSSIAN // else if (algorithm_d == GAUSSIAN) { status = computeGaussianNoise(output_a, channel_index_a); } // algorithm: SQUARE // else if (algorithm_d == SQUARE) { status = computeSquare(output_a, channel_index_a); } // algorithm: PULSE // else if (algorithm_d == PULSE) { status = computePulse(output_a, channel_index_a); } // algorithm: TRIANGLE // else if (algorithm_d == TRIANGLE) { status = computeTriangle(output_a, channel_index_a); } // error: unknown implementation // else { return Error::handle(name(), L"compute", ERR_UNKIMP, __FILE__, __LINE__); } // increment the number of frame // accum_frame_d(channel_index_a) = accum_frame_d(channel_index_a) + (Long)1; // possibly display the data // display(output_a, input_a, name()); // exit gracefully // return status; }// method: computeSine//// arguments:// VectorFloat& sine_wave: (output) sine wave// long channel_index: (input) channel index//// return: a boolean value indicating status//// this method computes sine wave sample points//boolean Generator::computeSine(VectorFloat& sine_wave_a, long channel_index_a) { // check if the frequency is not zero // if (frequency_d == (Float)0) { return Error::handle(name(), L"computeSine", ERR_ZFREQ, __FILE__, __LINE__); } // check if the sample frequency is at least twice the signal // frequency // if ((Float)sample_freq_d < (2 * frequency_d)) { return Error::handle(name(), L"computeSine", ERR_DTYPE, __FILE__, __LINE__); } // local variables // long length; long length_index; long last_frame_length = 0; length = (long)(Integral::round(frame_dur_d * sample_freq_d)); length_index = (long)(Integral::round(frame_dur_d * sample_freq_d)); // set the size of the output vector // sine_wave_a.setLength(length); long index = (long)accum_frame_d(channel_index_a) * length_index; long last_frame_index = (long)(Integral::ceil(signal_duration_d / frame_dur_d)); // dealing with computing last frame // if (accum_frame_d(channel_index_a) >= last_frame_index - 1) { // computing last frame length // last_frame_length = (long)(signal_duration_d * sample_freq_d - frame_dur_d * sample_freq_d * accum_frame_d(channel_index_a)); // compute the sine wave // for (long i = index; i < (index + length); i++) { // compute the sine signal samples only if the angle is positive // and the length is equal to the last partial frame length // if ((2.0 * Integral::PI * frequency_d * i / ((double)sample_freq_d) + phase_d) > 0 && (i - index) < last_frame_length) { // compute the sine samples // sine_wave_a(i - index) = bias_d + amplitude_d * (Float)sin(2.0 * Integral::PI * frequency_d * i / (long)(sample_freq_d) + phase_d); } // else the sine samples are zero // else { sine_wave_a(i - index) = 0.0; } } } // dealing with computing other frames // else { // compute the sine wave // for (long i = index; i < (index + length); i++) { // compute the sine signal samples only if the angle is positive // if ((2.0 * Integral::PI * frequency_d * i / ((double)sample_freq_d) + phase_d) > 0) { // compute the sine samples // sine_wave_a(i - index) = bias_d + amplitude_d * (Float)sin(2.0 * Integral::PI * frequency_d * i / (long)(sample_freq_d) + phase_d); } // else the sine samples are zero // else { sine_wave_a(i - index) = 0.0; } } } // exit gracefully // return true;}// method: computeGaussian//// arguments:// VectorFloat& gaussian_noise: (output) gaussian noise// long channel_index: (input) channel_index//// return: a boolean value indicating status//// this method computes Gaussian Noise sample points//boolean Generator::computeGaussianNoise(VectorFloat& gaussian_noise_a, long channel_index_a) { // define local variables // long signal_length; signal_length = (long)(Integral::round(frame_dur_d * sample_freq_d)); // set the length of the ouput vector // gaussian_noise_a.setLength(signal_length); for (long i = 0; i < signal_length; i++) { // compute the gaussian noise // gaussian_noise_a(i).grand(mean_d, variance_d); } // exit gracefully // return true;} // method: computeSquare//// arguments:// VectorFloat& square_wave: (output) square_wave// long channel_index: (input) channel_index//// return: a boolean value indicating status//// this method computes sine wave sample points//boolean Generator::computeSquare(VectorFloat& square_wave_a, long channel_index_a) { // check if the frequency is not zero // if (frequency_d == (Float)0) { return Error::handle(name(), L"computeSquare", ERR_ZFREQ, __FILE__, __LINE__); } // check if the sample frequency is at least twice the signal // frequency // if ((Float)sample_freq_d < ( 2 * frequency_d)) { return false; } // compute the number of samples in a frame // long length; length = (long)(Integral::round(frame_dur_d * sample_freq_d)); // set the length of the output vector // square_wave_a.setLength(length); // declare local variables // float square_width; float square_duration; long shift_samples; float ratio; // compute the width, duration, shift and ratio of the square wave // square_width = ((float)duty_cycle_d * (float)sample_freq_d) / ((float)frequency_d * 100.0); square_duration = (double)sample_freq_d / frequency_d; shift_samples = (long)((long)accum_frame_d(channel_index_a) * (double)sample_freq_d * (double)frame_dur_d + phase_d * square_duration / (2.0 * Integral::PI) + 0.5); ratio = shift_samples / square_duration; long k = (long)ratio + 1; long last_frame_length = 0; long last_frame_index = (long)(Integral::ceil(signal_duration_d / frame_dur_d)); // dealing with computing last frame // if (accum_frame_d(channel_index_a) >= last_frame_index - 1) { last_frame_length = (long)(signal_duration_d * sample_freq_d - frame_dur_d * sample_freq_d * accum_frame_d(channel_index_a)); // case when shift is greater then or equal to zero // if (shift_samples >= 0) { for (long i = 0; i < last_frame_length; i++) { // compute the positive portion of the square wave // if (((shift_samples + i) >= (long)((k - 1) * square_duration + 0.5)) && ((shift_samples + i) < (long)((k - 1) * square_duration + square_width + 0.5))) { square_wave_a(i) = amplitude_d * (Float)0.5 + bias_d; } // compute the negative portion of the square wave // else { square_wave_a(i) = amplitude_d * (Float)(-0.5) + bias_d; } if (k * square_duration == shift_samples + i + 1) { k = k + 1; } } } // case when shift is less than zero // if (shift_samples < 0) { for (long i = 0; i < last_frame_length; i++) { // set square wave signal point to zero when the x coordinate of the // point < 0 // if ((shift_samples + i - 1) < 0) { square_wave_a(i) = 0.0; k = 0; } // else the normal case // else { // compute the positive portion of the square wave // if (((shift_samples + i) >= (long)((k - 1) * square_duration + 0.5)) && ((shift_samples + i) <= (long)((k - 1) * square_duration + square_width + 0.5))) { square_wave_a(i) = amplitude_d * (Float)0.5 + bias_d; } // compute the negative portion of the square wave // else { square_wave_a(i) = amplitude_d * (Float)(-0.5) + bias_d; } } // increment the counter // if ((k * square_duration) == (shift_samples + i)) { k = k + 1; } } } // set square wave signal point to zero for the unspecified points of // last frame // for (long i = last_frame_length; i < length; i++) { square_wave_a(i) = 0.0; } } // dealing with computing other frames // else { // case when shift is less than zero // if (shift_samples >= 0) { for (long i = 0; i < length; i++) { // compute the positive portion of the square wave // if (((shift_samples + i) >= (long)((k - 1) * square_duration + 0.5)) && ((shift_samples + i) < (long)((k - 1) * square_duration + square_width + 0.5))) { square_wave_a(i) = amplitude_d * (Float)0.5 + bias_d; } // compute the negative portion of the square wave // else { square_wave_a(i) = amplitude_d * (Float)(-0.5) + bias_d; } // increment the counter // if (k * square_duration == shift_samples + i + 1) { k = k + 1; } } } // case when shift is less than zero // if (shift_samples < 0) { for (long i = 0; i < length; i++) { // set square wave signal point to zero when the x coordinate of the // point < 0 // if ((shift_samples + i - 1) < 0) { square_wave_a(i) = 0.0; k = 0; } else { // compute the positive portion of the square wave // if (((shift_samples + i) >= (long)((k - 1) * square_duration + 0.5)) && ((shift_samples + i) <= (long)((k - 1) * square_duration + square_width + 0.5))) { square_wave_a(i) = amplitude_d * (Float)0.5 + bias_d; } // compute the negative portion of the square wave // else { square_wave_a(i) = amplitude_d * (Float)(-0.5) + bias_d; } } // increment the counter // if ((k * square_duration) == (shift_samples + i)) { k = k + 1; } } } } // exit gracefully // return true;}// method: computePulse//// arguments:// VectorFloat& pulse_wave: (output) pulse_wave// long channel_index: (input) channel_index //// return: a boolean value indicating status//// this method computes pulse wave sample points//boolean Generator::computePulse(VectorFloat& pulse_wave_a, long channel_index_a) { // check if the frequency is not zero // if (frequency_d == (Float)0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -