MATLAB Answers

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

4 ビュー (過去 30 日間)
David Besonen
David Besonen 2020 年 3 月 9 日
コメント済み: David Besonen 2020 年 3 月 12 日
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.

  0 件のコメント

Sign in to comment.


Brian Hemmat
Brian Hemmat 2020 年 3 月 10 日
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 件のコメント

表示 2 件の古いコメント
David Besonen
David Besonen 2020 年 3 月 10 日
MATLAB Version: (R2019b) Update 4
MATLAB License Number: 866198
Operating System: Mac OS X Version: 10.15.3 Build: 19D76
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
Audio Toolbox Version 2.1 (R2019b)
Looks like version 2.1. I am using MatLab R2019b. Is there an older/newer version that wouldn't give me this trouble?
Brian Hemmat
Brian Hemmat 2020 年 3 月 10 日
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);
And then call this:
codegen getPitch -args {mySignal}
David Besonen
David Besonen 2020 年 3 月 12 日
Thank you very much, that seemed to work!

Sign in to comment.

その他の回答 (0 件)




Translated by