C-Code Error: Pitch Method Conversion to C Won't Work

1 view (last 30 days)
I am trying to convert MatLab's pitch method to C using the MatLab Coder app, but I am running into an error I do not understand. I am trying to convert this method to C for use in a real-time pitch shifter program that will work much better in C than it does in MatLab. I'm using the pitch method in the main script for the pitch-shifter. Here is how I am trying to convert the pitch method to C so far:
I open the Coder app and input the following script to auto-define the variable types of the pitch method. In this example, mySignal is a 1024x1 double.
pitch(mySignal, 1852.2, 'NumChannels', size(1024, 2), 'SamplesPerChannel', size(1024, 1), 'Range', [50,400], 'WindowLength', round(1852.2 * 0.052), 'OverlapLength', round(1852.2 * 0.042), 'Method', 'NCF', 'MedianFilterLength', 1);
I then get the following auto-defined variable definitions:
From here, I attempt to run the MEX test generator to detect errors. The following image details the error I receive:
There is some issue with my auto-defined variables and the MatLab setProperties() method:
Any help you can give on this, I greatly appreciate. I need this for a school project in less than a month and I am just at a loss for what the problem is. The method should be able to be converted, as it contains the C Generation tag on its MatLab documentation page. Thank you for your help, please let me know if you need any clarification.

Accepted Answer

Brian Hemmat
Brian Hemmat on 10 Mar 2020
Hi David,
You're specifying a couple name-value pairs that aren't actually parameters of the pitch function (NumChannels, SamplesPerChannel). The parameters of the function are documented here.
Also, it looks like you're specifying a number of parameters that you don't have to (since you are just specifying their defaults). The following generates code for me, and I believe is what you're after:
mySignal = pinknoise(1024);
fs = 1.8522e+03;
codegen pitch -args {mySignal,fs}
If your project does not require you to actually implement the pitch shifting, you might also look at the shiftPitch function (which you can generate code from) or the audiopluginexample.PitchShifter (in the Audio Plugin Gallery). You can generate a VST plugin from the audiopluginexample.PitchShifter.
  5 Comments
Brian Hemmat
Brian Hemmat on 10 Mar 2020
Hi David,
The pitch function requires the sample rate to be a constant. You can assert that the sample rate is a constant using coder.Constant. The following generates code for me in 19b:
mySignal = pinknoise(1024);
fs = 1.8522e3;
codegen pitch -args {mySignal,coder.Constant(fs)}
However, this means that you will need to call the generated code with the sample rate even though its a constant:
f0 = pitch_mex(mySignal,fs)
Alternatively, you could wrap the pitch function in another function, hard-code the sample rate, and then generate that function. For example, save this locally:
function f0 = getPitch(x)
fs = 1.8522e3;
f0 = pitch(x,fs);
end
And then call this:
codegen getPitch -args {mySignal}

Sign in to comment.

More Answers (0)

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!