# C-script block in Frequency Response Analysis (FRA)

**URL:** <https://forum.plexim.com/t/c-script-block-in-frequency-response-analysis-fra/3329>\
**Category:** PLECS\
**Tags:** c-script, frequency-response, microprocessors\
**Created:** [September 29, 2026, 9:33pm UTC](https://forum.plexim.com/t/c-script-block-in-frequency-response-analysis-fra/3329 "2026-09-29T21:33:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Omar](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@Omar](https://forum.plexim.com/u/Omar)\
**Post date:** [September 29, 2026, 9:33pm UTC](https://forum.plexim.com/t/c-script-block-in-frequency-response-analysis-fra/3329/1 "2026-09-29T21:33:39Z")

</div>

Hello all,

In the attached model, I’m trying to compare the frequency response of an analogue 3P3Z (type-III) controller to it’s discrete counter-parts (two versions: MMPZ and Tustin), but the discrete versions seems way off around the crossover frequency (8 kHz), I suspect I didn’t set up the C block correctly, any chance it has to do with the discreteStates(0,0)?

Also why would frequency response generate a smooth bode plot, while the AC sweep produces a pretty jittery one?

@Bryan_Lieblick , this is coming from the previous post:

> [@How to use script to run multiple AC sweep simulations?](https://forum.plexim.com/t/how-to-use-script-to-run-multiple-ac-sweep-simulations/879/9):
>
> Many thanks for your support, while @Bryan_Lieblick response is quite informative and provides more detailed solution using scripts, for the current application on hand I used the solution proposed by @Munadir_Ahmed since it’s much faster to set up, and I don’t need more at the moment. Neverthelss, I’ll try to implement @Bryan_Lieblick approach, at least to have it ready for future reference. I have attached my work in case you want to have a look, and for sure any comment or advice is highly …

[discreteFunctionFrequencySweep\_R02.plecs](https://forum.plexim.com/uploads/short-url/tsRozFWbG3qfL0ANllmnoA2rhqt.plecs) (30.3 KB)

Many thanks in advance,

Omar

---

<div class="post-metadata">

**Author:** ![Bryan\_Lieblick](https://dub1.discourse-cdn.com/flex006/user_avatar/forum.plexim.com/bryan_lieblick/32/10_2.png) [@Bryan\_Lieblick](https://forum.plexim.com/u/Bryan_Lieblick)\
**Post date:** [September 30, 2026, 2:31pm UTC](https://forum.plexim.com/t/c-script-block-in-frequency-response-analysis-fra/3329/3 "2026-09-30T14:31:50Z")

</div>

Hi Omar,

Your diagnosis is correct. The methods below rely on accurate accounting of the system states. When you do not explicitly define states as such in the C-Script, then the PLECS solver doesn’t know they are states.

- [AC Sweep](https://docs.plexim.com/plecs/latest/analysis/ac-sweep/) uses states for steady-state convergence.
- [Frequency Response Analysis](https://docs.plexim.com/plecs/latest/analysis/frequency-response/) uses it to store the state from the initial run.

Fixing the state issue, the Frequency Response Analysis (FRA) and Multitone both return consistent results. There also was an issue in your FRA setup where the settling time span was set to `0`. This parameter is the “Time needed by the system to reach steady state after the sinusoidal perturbation is applied, in seconds (s).” and should be non-zero. It’s based on your system dynamics, but I chose 0.01 sec and it looked fine. Also note that multitone would have worked fine from the start because it doesn’t rely on the stored system state, but rather two transient runs.

The AC Sweep has some issues converging because it has a pure integrator, as alluded to in this [previous forum post](https://forum.plexim.com/t/obtaining-frequency-response-of-a-dll-block/1811/4), so it isn’t a great method for this application. If you set the AC Sweep’s “Operating Point” to “non-periodic (DC)” then the analog TF extraction works, but the digital ones don’t. Generally, AC Sweep is superceded by FRA since it side-steps many of the steady-state convergences issues that limit that method.

Regards,  
Bryan

[discreteFunctionFrequencySweep\_R02\_BL.plecs](https://forum.plexim.com/uploads/short-url/vhJPJVZQEHbIfbcWbF11v59dUdW.plecs) (30.7 KB)

---

<div class="post-metadata">

**Author:** ![Omar](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@Omar](https://forum.plexim.com/u/Omar)\
**Post date:** [October 1, 2026, 11:29pm UTC](https://forum.plexim.com/t/c-script-block-in-frequency-response-analysis-fra/3329/4 "2026-10-01T23:29:58Z")

</div>

Dear @Bryan_Lieblick ,

I really appreciate the detailed explanation and the revised model, they are really helpful resources and allows better understanding of the different applications of analysis tools.

I have ran the provided model, and I still see that there is a big difference in the phase magnitude between the analogue TF and it’s C-implemented variants, specifically at 8kHz (frequency of interest):

 ![image](https://europe1.discourse-cdn.com/flex006/uploads/plecs/original/2X/f/f4cb3dc792bd0e041e48c6d5f6f449be896b7746.png)

What are your insights on that? given that I have discretized the TF using MMPZ and pre-warped Tustin at Ts=1/200e3.

Many thanks again for all your help and support.

Omar
