Matlab & Gen

bearded clumsybear's icon

I'm getting more and more into gen~ and learning to use the codebox and it's syntax.
For now, I'm trying to convert rather simple Matlab code to genexpr, although with little to no progress.

How would one go about converting this piece of Matlab code to genexpr?

% Diode clipper
% Implementation by Nestor Napoles Lopez, December 2018
% based on the paper by Yeh et al. (2007)

function x = diodeclip(x)
n = 2.5;
for i=1:length(x)
x(i) = x(i) / (1 + abs(x(i)).^n).^(1/n);
end
end


I know about using pow in genexp. I have no clue how "function x = diodeclip(x)" should translate to gen though.
i=1:length(x) -> I assume x is the number of samples?
where is in1 and out1 supposed to be in this code.

here is the repository I took the code snippet from. It's about a BOSS Distortion pedal emulation.
https://napulen.github.io/reports/mcgill/mumt618/#diode-clipper

the clipper snippet is the simplest, that's why I chose it first. Next would be the filter and op-amp stage if I'm getting the hang of it :)

Any input would be appreciated, thanks

bearded clumsybear's icon

I think I understand now. So if I'm not mistaken this line of Matlab code:

x(i) = x(i) / (1 + abs(x(i)).^n).^(1/n);


translates to this in gen:

out1 = in1 / (1+(pow((pow((abs(in1)),2.5)),0.4)));

anything else I don't think I need from the Matlab snippet, right?

Luigi Castelli's icon

Close but not quite, beware of your parentheses and you probably want to make n into a parameter called "linearity" or something.

out1 = in1 / pow(1.0 + pow(abs(in1), n), 1.0 / n);

The function has a cool shape and n will control the amount of distortion you get.
Check it out:
https://www.desmos.com/calculator/zowfltcld2

- Luigi

bearded clumsybear's icon

Thanks for clarifying, Luigi! I implemented your correction and suggestion and it sounds a lot better now :)

I'm at a stage where I'd like to implement the Transistor Booster Stage of the Boss DS-1 emulation, but I'm not quite sure what to make of A = [A0, A1 ,A2];
B = [B0, B1, B2];
I understand that A / B are filter coefficients, but I don't know what it means that they get calculated by their respective array of A1/A2/A2 or B0/B1/B2.
Also, what type of filter is the standard filter function in Matlab?

% Transistor Booster Stage
% Implementation by Nestor Napoles Lopez
% based on the paper by Yeh et al. (2007)

function y = bjtfilt(x, fs)

% After working the math, I put all the discrete-time
% coefficients in terms of this variable coeff
coeff = pi/fs;

B0 = 1;
B1 = -2;
B2 = 1;

A0 = 1800.*coeff.^2 + 603.*coeff + 1;
A1 = 3600.*coeff.^2 - 2;
A2 = 1800.*coeff.^2 - 603.*coeff + 1;

% We obtain the gain from
% 36dB = log10(x) * 20
amp = 10.^(36/20);
B = amp .* [B0, B1, B2];
A = [A0, A1, A2];

y = filter(B, A, x);

end

Luigi Castelli's icon

Those are biquad coefficients.
Look into the [biquad~] object in Max/MSP.
There are also some filter tutorials that Cycling74 just released.

Study those and you will get a good grasp on what you need to get going.
As always, enjoy the process... :)

- Luigi

Max Gardener's icon

Those coefficients look like a biquad to me, although I've been spending quality time with Isabel K.'s tutorial, so I may be pre-conditioned to see biquads where they don't exist.

Have you considered actually looking up the original paper? I'll bet my lunch pint it's David Yeh's work (he is/was at Stanford). Try googling Yeh filter 2007 and see what you get. Might be helpful.

bearded clumsybear's icon

Yes, I was thinking of this being a biquad filter because of the 3 coefficients for numerators and denominator. I used the gen~ biquad example and pugged in those values, but with no success.

This is what I got now:

here is the patch if you wanna try it out

Max Patch
Copy patch and select New From Clipboard in Max.

bearded clumsybear's icon

did I get something completely wrong here?

Isabel Kaspriskie's icon

I'll give it a +1 on the biquad nomenclature. :)

Peeking at the original post's link, there's a reference to the paper here (Indeed, it's David Yeh.): https://ccrma.stanford.edu/~dtyeh/papers/yeh07_dafx_distortion.pdf

I also want to put a recommendation out for looking at the official MATLAB documentation! They have some useful information on scientific/mathematical computing, even if you're not writing or translating MATLAB code. For the filter function: https://www.mathworks.com/help/matlab/ref/filter.html

y = filter(b,a,x) filters the input data x using a rational transfer function defined by the numerator and denominator coefficients b and a.

bearded clumsybear's icon

Here is another equation I'm trying to transfer to gen~

Is = 10e-15;
eta = 1;
Vt =0.026;

y(n,1) = Is * (exp (x(n,1)/(eta*Vt)) - 1);

Would it be correct to assume that the equation would transfer to following with the same values gen~ ?

out1 = Is * (exp (in1/(eta*Vt)) - 1);

I'm not getting any signal with the above values. only if I modify them drastically. Does the scientific notation translate from Matlab code to gen~ like that or do I have to change something?

phelankane's icon

Hiya

This appears to be similar to my friend Dr Eric Tarr's Diode Distortion code from his book An Introduction to Computer Programming and Digital Signal Processing in MATLAB.

I have ported many of his algo's over to Gen for a forth coming Getting Confident with Gen course I will be delivering at Music Hackspace. This will run in the future after our getting Started With Gen has run (please see my other post for deets). In the mean time here's a working patch. It seems that you have used different coeff's values in your pseudo code but you could reverse engineer my patch if it helps.

We will also soon be running some course at Music Hackspace with Eric. He's a great teacher.

Good luck.

P

Diode Distortion.zip
application/zip 6.31 KB

bearded clumsybear's icon

Hi Phelankane!
yes that's exactly where it's from, I'm using Eric Tarr's book extensively throughout my DSP learning journey :) It is a great resource and yes, he is a very good teacher!

Thanks for your reply and the patch! Any idea on when you will roll out the Getting Confident With Gen course?

phelankane's icon

Hiya

Getting Started with Gen is scheduled for May so I guess June / July time. I am currently designing the curriculum for it. C74's Gregory Taylor has graciously been a useful spring board during my curriculum design.

Here's a rough overview of its content (subject to change):

Getting Confident with Gen (name TBC) Intermediate to Advanced

All graphical: flanging, chorusing, multi tap delays, ping pony delays, pitch shifting, distortion, IIR biquad filters i.e. LPF, HPF, BPF, APF, shelf, notch, peak, calculating filter coeffs from RBJ cookbook equations, phasors, emulating classic patches from my Eventide H8000FW and VSig

I'll post in the Gen forum when its announced so keep an eye out!

Good luck

P