Generating pwlech with matlab coder for C language

I am trying to generate c langauge code for two-sided Welch PSD function : [pxx,f] = pwelch(x,window,noverlap,f,fs)
I generated and compiled the code successfully but when I try to run the given example without modifying it in an IDE (codeblocks) I got segmentation fault error specifically in generated function psdfreqvec()

6 Comments

It is difficult to answer the question without more context. What is the example you are trying to follow ?
I hope you are following the generated example main() to check the usage of generated code :
Please attach the sample main() function of your application to verify the types of the inputs.
Here is the sample of main()
#include <stdio.h>
#include <stdlib.h>
#include "DataCalibration.h"
#include "DataCalibration_emxAPI.h"
#include "DataCalibration_terminate.h"
#include "rt_nonfinite.h"
#include <string.h>
/* Function Declarations */
static emxArray_real32_T *argInit_Unboundedx1_real32_T(void);
static float argInit_real32_T(void);
int main()
{
emxArray_creal32_T *x_cal;
emxArray_real32_T *x;
float fs_tmp;
emxInitArray_creal32_T(&x_cal, 1);
/* Initialize function 'DataCalibration' input arguments. */
/* Initialize function input argument 'x'. */
x = argInit_Unboundedx1_real32_T();
fs_tmp = argInit_real32_T();
/* Call the entry-point 'DataCalibration'. */
DataCalibration(x, fs_tmp, fs_tmp, x_cal);
emxDestroyArray_creal32_T(x_cal);
emxDestroyArray_real32_T(x);
printf("Hello world!\n");
return 0;
}
/*
* Arguments : void
* Return Type : emxArray_real32_T *
*/
static emxArray_real32_T *argInit_Unboundedx1_real32_T(void)
{
emxArray_real32_T *result;
static const int iv[1] = { 2 };
int idx0;
/* Set the size of the array.
Change this size to the value that the application requires. */
result = emxCreateND_real32_T(1, iv);
/* Loop over the array to initialize each element. */
for (idx0 = 0; idx0 < result->size[0U]; idx0++) {
/* Set the value of the array element.
Change this value to the value that the application requires. */
result->data[idx0] = argInit_real32_T();
}
return result;
}
/*
* Arguments : void
* Return Type : float
*/
static float argInit_real32_T(void)
{
return 0.0F;
}
Here is the matlab code
function x_cal = DataCalibration(x,fs,dBref)
% x: calibration data in time domain
% fs: sampling frequency
% dBref: Reference dB for calibration
% Reference
Pref = 20e-6;
x_FFT = pwelch(x,fs,fs/2,fs,fs);
% Measured Signal dB
dB_cal = 10*log10(sum(x_FFT)/Pref^2);
% Calibration Factor
calFactor = 10^((dBref-dB_cal)/20);
% Calibrated signal
x_cal = x * calFactor;
Thank you for providing us with the MATLAB code. As a first troubleshooting step, could please re-generate the code with runtime checks enabled as described in the link below:
Enabling runtime checks will ensure that the generated code reports runtime issues via a call to 'fprintf' before aborting the program. If runtime checks are not enabled, then a runtime error may manifest as a segmentation fault. Runtime issues usually indicate that the input you provided to the entry point function was somehow ill-formed or unexpected.
If enabling runtime checks does not help resolve the issue, then could you please provide us with the commands you used to generate the code (such as the codegen command and any commands used to prepare a config object or entry point types)?
If you used the MATLAB Coder app to generate the code, could you use the project-to-script feature described in the link below to produce a codegen script and send it to us?
Also, make sure that you call the generated code's terminate function at the end of your main to ensure that the program is terminated correctly. In this case, the terminate function should be called 'DataCalibration_terminate'.
Hi,
Could you please share the inputs x ,fs and dBref that were used.
dBref = 64;
fs = 48000;
x = textread('speech.txt');
Hi,
Any updates on the issue?

Sign in to comment.

Answers (0)

Products

Release

R2020a

Asked:

on 9 May 2021

Commented:

on 6 Jun 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!