Input signal

In this section we look at the input signal and the steps to work with it.

  1. After setting up the UI and the Java-to-native bindings, extend native-lib with the generateSignal method, as you can see here:

    #define SIGNAL_LENGTH 1024
    #define SIGNAL_AMPLITUDE 100
    #define NOISE_AMPLITUDE 25
     
    int8_t inputSignal[SIGNAL_LENGTH];
     
    void generateSignal() {
        auto phaseStep = 2 * M_PI / SIGNAL_LENGTH;
    
        for (int i = 0; i < SIGNAL_LENGTH; i++) {
            auto phase = i * phaseStep;
            auto noise = rand() % NOISE_AMPLITUDE;
    
        inputSignal[i] = static_cast(SIGNAL_AMPLITUDE * sin(phase) + noise);
        }
    }
    
    This method generates a noisy sine wave which you can see the blue curve in the image of the app UI shown in the screenshot earlier. This stores the result in the inputSignal global variable.
    Control the signal length, its amplitude, and noise contribution using the SIGNAL_LENGTH, SIGNAL_AMPLITUDE, and NOISE_AMPLITUDE macros. However,inputSignal is of type int8_t. Setting too large amplitudes result in integer overflow.
  2. Invoke the generateSignal method in the exportable C function, to pass the generated signal to the Java code for plotting. This function was previously executed by Android Studio:

    extern "C"
    JNIEXPORT jbyteArray JNICALL
    Java_com_example_neonintrinsicssamples_MainActivity_generateSignal(
        JNIEnv *env, jobject /* this */) {
    
        generateSignal();
    
        return nativeBufferToByteArray(env, inputSignal, SIGNAL_LENGTH);
    }
    
  3. There is a new method here: nativeBufferToByteArray This method takes the pointer to the native C++ array and copies array elements to the Java array. (see this link for more information):

    jbyteArray nativeBufferToByteArray(JNIEnv *env, 
    int8_t* buffer,int length){ auto byteArray = env->NewByteArray(length); env->SetByteArrayRegion(byteArray, 0, length, buffer); return byteArray; }
Previous Next