Scientific Computing

Matlab exit return code for CI

Continuous integration (CI) systems generally rely on an integer return code to detect success (== 0) or failure (!= 0). The error() and assert() functions of Matlab / GNU Octave return non-zero status that works well with CI systems. The Matlab unittest framework is the primary method for testing Matlab code.

Matlab -batch option to run from the command line completely replaces -r and is more robust.

matlab -batch "assertSuccess(runtests)"

For pre-buildtool releases of matlab (< R2022b) we use a helper script that invokes runtests with appropriate parameters for the Matlab release used. To test across Matlab releases without a specific CI Matlab version fanout, we can use Python Pytest as in this wrapper script that tests Matlab from R2017a to R2025b and beyond.

Reboot computer from terminal command

There are several ways to reboot a computer from the terminal. For Linux or macOS the “reboot” command (or shutdown -r) is commonly used. For Windows, the PowerShell command Restart-Computer is a standard way to reboot the system from Terminal.

Within Windows Subsystem for Linux (WSL), the “reboot” command is only for the particular WSL instance and actually results in a shutdown of the WSL instance, not the entire Windows system. One can verify this by before and after the standard 8 second WSL shutdown time, running in Windows Terminal:

wsl.exe --list --running

Anytone AT-5000 CPS Com Port Driver

The Anytone AT-5000 radio has three modes: Ham, HF, and CB. 99 memories are available in each of Ham mode and HF mode. 16 memories are available in CB mode (even after moving the jumper that enables Ham and HF modes). MEM channels are not shared between frequency modes, so there are 99 Ham and 99 HF channels and 16 CB MEM channels. The radio cover screws are T9 Torx.

Anytone AT-5000 quirks:

  • “Dual watch” does not work in VFO mode, even from MEM channels. VFO is only available in Ham or HF profiles, and is how one would normally use the radio in HF or Ham mode.
  • If a MEM channel is deleted from the scan list, it’s still scanned when scanning MEM channels. This seems like a firmware bug, present in a radio with LCD version 1.00, RF version 1.00, and radio data version 1.00. The “workaround” is to delete the channel from MEM, which is not so desirable but the only way we know how at this time.

CPS programming

Anytone (Qixiang Electronics) provides a programming cable for the Anytone AT-5000 radio, which connects via the 8P8C modular microphone jack to USB. This is a USB to serial adapter. If the comm port does not appear in the AT-5000 CPS software or in Device Manager under “Ports (COM & LPT)” as “USB-SERIAL CH340 (COMx)”, check Windows Device Manager under “Other devices” for “USB Serial” with a yellow warning icon.

The driver necessary is the GD USB Virtual Comm Port driver, which can be downloaded as part of the Anytone D878 programming software package if not seen as part of the Anytone AT-5000 CPS install package. Don’t install the D878 CPS software, just extract the Zip archive folder titled like “Virtual GD USB Com Drivers (Only If Needed)” and extract the “GD_VirtualComDriver v2.0.2.4944.rar” file to get the driver installer under the “x64” folder. After installing the driver, unplug and replug the programming cable. If it’s still showing a yellow warning icon, right-click on the “USB Serial” entry, select “Update driver”, and choose to browse for drivers on the computer by “let me pick from a list of available drivers on my computer”. Under USB Devices look for “USB-SERIAL CH340” and select it to install the driver. It might be necessary to unplug and replug the programming cable again after installing the driver for it to be recognized properly. There should no longer be a yellow warning icon, and the comm port should appear in the AT-5000 CPS software as “USB-SERIAL CH340 (COMx)” under the “Port” dropdown menu.

Workaround for CPS bugs

These bugs were present in the first release of the AT-5000 CPS software.

  • Can’t add or edit memory channels: set the band and working band near the top of the Channel editing window to MEM. Each time a channel is edited, this may need to be done again
  • Can’t delete memory channels: channels appear to be deleted, but appear again upon programming the radio. Simply delete them from the radio front panel instead

CMake Ninja show link commands

CMake can show the compile commands that will be issued to compile targets by setting the CMAKE_EXPORT_COMPILE_COMMANDS variable to ON. This generates ${CMAKE_BINARY_DIR}/compile_commands.json file that contains the JSON compilation database for each source file. However, this does not include the link commands used to link the final executable or library.

There are a couple ways to make the build system emit the link commands without actually yet linking the target:

Ninja JSON compilation database method

To show the compile and link commands in JSON compilation database format using Ninja for a specific target including target dependencies use the compdb-targets Ninja tool.

cmake -B build -G Ninja

cmake --build build -- -t compdb-targets MyTarget > compile_and_link_commands.json
# equivalent to
ninja -C build -t compdb-targets MyTarget > compile_and_link_commands.json

GNU Make or Ninja dry-run method

The Ninja compdb-targets method above is preferred, since the drawbacks of this “dry-run” method include:

  • have to clean-first to show all the dependecies, otherwise if some dependencies have been built already, they won’t be shown in the output.
  • the output is not in JSON format, so it may be harder to parse.

The -n dryrun flag shows the compile and link commands without executing them.

cmake -B build

cmake --build build --target mytarg --verbose --clean-first -- -n

Matlab reset preferences

Matlab preferences are stored in <prefdir>/matlab.mlsettings since Matlab R2020a. To reset Matlab preferences, one can rename / move this file–keep it for a backup. The location of <prefdir> can be found by running the following command in Matlab:

prefdir

After moving the matlab.mlsettings file, restart Matlab. Matlab will create a new matlab.mlsettings file with default preferences.

This is particularly helpful if Python is setup with a “bad” distribution that crashes Matlab upon Python py. commands in Matlab, and there isn’t another Python distribution available to switch to in Matlab’s preferences.

The matlab.mlsettings file is a ZIP file of XML and JSON files and folders.

Find largest directories one-liner

These one-line Unix-like shell commands help find the directories consuming the most hard drive space. This command lists the top 10 largest directories in the specified path. This is useful to HPC where disk quota as seen by quota -s or similar indicates it’s time to clean up some files.

du -hd1 /path/to/check 2>/dev/null | sort -rh | head

While disk size quota is often the main concern, there is often also a quota on the number of files (inodes) that can be owned by a user. To find the directories with the most files, use this command:

find /path/to/check -type f -printf '%h\n' 2>/dev/null | sort | uniq -c | sort -rn | head

Graphical terminal disk usage program “ncdu” is a widely-available Ncurses based program showing the largest files. NCDU is more user-friendly than the plain text method above. NCDU allows deleting files interactively. When using on WSL, specify the desired drive like (for C: in this example):

ncdu /mnt/c

CMake add_custom_target Windows nuisance rebuild

When using CMake’s add_custom_command and add_custom_target on Windows, if the CMAKE_EXECUTABLE_SUFFIX (which is typically .exe) is not included as part of the command or target, it can lead to the custom target is rebuilt every time the project is rebuilt, even if the command is up to date. This happens because CMake does not exactly match the output file of the custom command, since the .exe suffix was missing on Windows, and therefore CMake does not properly track the existence of a target’s dependencies. As a result, CMake assumes that the target is always out of date and needs to be rebuilt. To avoid this issue, ensure that the output of your custom command includes the CMAKE_EXECUTABLE_SUFFIX. For example, if generating an executable with add_custom_command, specify the output file with the correct suffix:

set(myexe ${CMAKE_CURRENT_BINARY_DIR}/my_executable${CMAKE_EXECUTABLE_SUFFIX})

add_custom_command(
    OUTPUT ${myexe}
    COMMAND ...
)

add_custom_target(MyTarget ALL
    DEPENDS ${myexe}
)

By including the CMAKE_EXECUTABLE_SUFFIX in both the OUTPUT and DEPENDS sections, CMake will correctly recognize the target as an executable and will only rebuild it when necessary, thus avoiding unnecessary rebuilds.

Public WiFi SSL mangling

Check the desired website’s SSL certificate with a service like Qualys SSL Labs to see if the certificate is valid and properly configured. If the certificate is valid but you still encounter SSL errors, it’s possible that the public WiFi network is interfering with the SSL connection. Also try using command line web programs to see if there are any SSL errors or warnings in the output. Examples:

curl -v https://www.scivision.dev

python -c "import requests; print(requests.get('https://www.scivision.dev').status_code)"

If these fail, try without SSL

curl -v http://www.neverssl.com

python -c "import requests; print(requests.get('http://www.neverssl.com').status_code)"

An example of the curl output when even HTTP connections are interfered with:

curl -v http://neverssl.com
* Host neverssl.com:80 was resolved.
* IPv6: (none)
* IPv4: x.x.x.x
*   Trying x.x.x.x:80...
* connect to x.x.x.x port 80 from 0.0.0.0 port 58197 failed: Timed out
* Failed to connect to neverssl.com port 80 after 21104 ms: Could not connect to server
* closing connection #0
curl: (28) Failed to connect to neverssl.com port 80 after 21104 ms: Could not connect to server

CB Radio AM Auto-Squelch characteristics

Since the introduction by President Electronics of the patented Auto-Squelch feature in CB radios in 1998-1999, auto-squelch (a.k.a. ASC or ASQ) for AM and FM has become widespread in CB radios. The auto-squelch feature allows one to detect weaker signals than would be practical with a manual squelch when driving around or scanning channels because the background noise is automatically accounted for. Auto-squelch is not implemented for SSB, which remains on manual threshold setting of AGC-based squelch. Algorithms exist for voice-activated squelch for SSB, but they are generally not yet implemented in CB (or amateur) radios.

A key innovation was including RF signal strength along with the out-of-band baseband noise level commonly used in FM auto-squelch to determine the squelch threshold. This allows the squelch threshold to be set lower when a strong signal is present, which allows weaker signals to be heard. The auto-squelch algorithm is not perfect, however, and is often fooled into closing by overmodulated AM signals, which can cause the squelch to open and close rapidly, making the signal difficult to understand. The movement by prominent CB radio technicians to avoid overmodulation is to be lauded generally for not fruitlessly disrupting adjacent channel users, and especially to preserve the functioning of auto-squelch. The false closing of squelch even for on-channel strong overmodulated signals is due to the large amount of out-of-band distortion products generated by overmodulation, which can cause the auto-squelch algorithm to incorrectly determine that this is an undesired strong signal present and close the squelch.

In practice, one might use auto-squelch for general driving around and scanning channels, but switch to manual squelch when trying to listen to a known channel with a strong signal that is overmodulated. For reception of weak signals, switching to the manual squelch and leaving the squelch open is generally optimal. These characteristics are why it’s so important when buying a CB radio to get one that also has the “NRC” noise reduction algorithms, which make listening far less fatiguing on any voice mode AM / FM / SSB. It’s a shame that so many are blindly buying the “Cobra 29” style legacy radios when for the same price and often less money, they could get a CB radio with NRC and other features like scanning that make the CB radio experience much more enjoyable and productive.

AFCI circuit protection trip from amateur radio transmission

AFCI protection comes in the form of circuit breakers, outlets, and dead-front outlets that protect downstream circuits from arc faults, which can lead to fires. Today’s AFCI protection is typically Combination AFCI with GFCI, which detects series or parallel arc faults and provides ground fault protection. AFCI are designed to detect and interrupt electrical arcs that can lead to fires, but they can sometimes be sensitive to the electromagnetic interference (EMI) generated by radio transmissions. Amateur radio and CB radio operators have reported instances where their radio transmissions cause AFCI (Arc Fault Circuit Interrupter) circuit protection to trip. Many early reported false trips were due to RF getting into the circuit and AFCI protection. As noted in the linked articles, a first solution may be replacing the AFCI with a newer model that has improved false-trip reduction.

AFCI can also be sensitive to current variation implicit in single-sideband (SSB) transmissions, which can cause the circuit breaker to trip if the rapidly varying current is interpreted as a series arc fault. We have observed that once the current draw on an AFCI circuit is above a certain threshold (say 5-10 amps), the AFCI becomes sensitive to the rapidly varying current draw of an SSB transmitter and falsely trips–even in the transmitter is only drawing say 1-2 amps at 120 volts. This can happen from having an electric heater or other high-current device on the same circuit as the radio, which can cause the AFCI to trip when the radio is transmitting at the same time the high-current device is drawing power. This can be verified by using a battery power supply for the transmitter, to help ensure it’s not coupled RF causing the issue. If the AFCI is new, the only solution may be to put the steady high-current loads and SSB transmitter on separate circuits. AFCI protection should not be removed, as it has proven effective in preventing fires.