Scientific Computing

CMake compiler feature detect

In CMake projects (or other build systems) it’s more robust to detect compiler features rather than make brittle, difficult to maintain nested if / elseif logic trees based on compiler vendor and version. There are edge cases where if() statements to work around known compiler bugs are necessary, but where we can, we use compiler feature checks instead. Each compiler vendor such as AppleClang, GNU GCC, LLVM Clang, typically publishes tables of compiler features vs. language standard support in their documentation. These tables might not cover every aspect of feature support, or the project invocation of that support may trigger bugs in specific compiler versions. Then, either hard-coded build system logic or preferably configure-time feature detection is needed.

An example using check_source_compiles in a CMake project requires C11 variable length arrays. The CMakeLists.txt would look like:

include(CheckSourceCompiles)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
# so that check_source_compiles sets the correct language standard

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
# save link time, only compile is needed

check_source_compiles(C "int main(void){
for (int i = 1; i < 5; i++){
  int a[i];
}
return 0;
}"
c11vla)

if(NOT c11vla)
 # fatal error or disable feature using C11 VLA
endif()

add_executable(c11demo demo.c)

A similar meson.build configure-time compiler feature check example:

project('c11vla_demo', 'c',
  default_options : ['c_std=c11'])

c11_vla = meson.get_compiler('c').compiles(required: true, name: 'c11_vla',
  'int main(void){ for (int i = 1; i < 5; i++){ int a[i]; } return 0; }'
)

VHF marine FM narrowbanding impacts

12.5 kHz channel spacing is permitted in certain cases by the FCC in VHF marine radio. Despite experiments demonstrating the practicality of using 12.5 kHz channel spacing on VHF marine radio, currently available radios and practice appear to remain at +/- 5 kHz deviation and 25 kHz channel spacing. Unlike the vocal controversy around 1969 over marine VHF narrowbanding from +/- 15 kHz deviation to +/- 5 kHz deviation, there appears to be little controversy over going to 12.5 kHz marine VHF channel spacing. Assuming there isn’t adjacent channel interference, the communications range penalty from narrowbanding 5 kHz FM deviation systems to 2.5 kHz FM deviation can be represented to first order as 20*log10(2.5/5) + 20*log10(16/11) ~ -3.3 dB, which is measurable but not necessarily devastating for a system that already has adequate coverage. This comes from the disadvantage of reduced FM modulation index (2.5 / 5) and the advantage of narrower IF receiver filter (11 kHz vs. 16 kHz typical). The true significance must be measured and modeled for the particular communications systems. Keep in mind the reduced ability to withstand co-channel interference due to the reduced FM capture effect, which can be expressed in dB as the cube 10*log10((5/2.5)**3) (9 dB C/N reduction in resilience to co-channel interfering FM signals).

Unlike in commercial PMR / LMR / SMR systems where digital modulation gains of 3-4 dB can be achieved by say APCO25, DMR, NXDN or similar, VHF marine radio like airband VHF needs to keep compatible analog modulation schemes indefinitely due to the large number of casual spectrum users that still need life-critical communications access.

HDF5 CMake build

CMake is required to build HDF5 2.0 and newer, building static and dynamic HDF5 libraries under the user install prefix. The *stub files may not be present, depending on the build configuration. We omit the file suffixes for simplicity.

lib/libhdf5
lib/libhdf5_fortran
lib/libhdf5_hl
lib/libhdf5_hl_fortran
lib/libhdf5_hl_f90cstub
lib/libhdf5_f90cstub

The Fortran .mod files that need to be included are under

include/

One straightforward build/install example is:

cmake -B build --install-prefix=$HOME/hdf5
cmake --build build --parallel
cmake --install build

Tell CMake to use this HDF5 from a user project:

cmake -B build -DHDF5_ROOT=$HOME/hdf5

RHEL SSH remote desktop VNC

RHEL has particular quirks when it comes to remote desktop connections. For software development on a remote computer, it can be easier to use VSCode over SSH to the remote computer than VNC on RHEL. Here are common considerations for remote desktop access to RHEL and RHEL-derived Linux distros.

For SSH connection problems, consider adding to /etc/ssh/sshd_config:

LogLevel DEBUG

then:

systemctl restart sshd.service

then:

journalctl -f

and try to login. This will print a good amount of information streaming and helps reveal .ssh/authorized_keys permissions issues and more.

Check that firewalld is allowing the desired SSH port through.

tcpdump port 22 -n -Q inout

should show packets from the client–if not the SSH server firewall may be blocking them.

Install GNOME remote desktop server and follow the RHEL Remote Desktop setup. This procedure is completely different from legacy RHEL releases that used TigerVNC, since RHEL now uses Wayland.

dnf install gnome-remote-desktop

Fortran Array Temporary debugging

Fortran array temporaries can be implicitly created when non-contiguous array slices are passed to procedures. This copy of data can impact performance. Older compilers like GCC 8.x (including Gfortran 8.5), which may be the only installed choice on some HPC systems, have occasional bugs related to array temporaries that can lead to incorrect results or crashes. Compilers older than GCC 9 are not supported by Fortran-stdlib among other prominent Fortran libraries.

It is possible that disabling optimization can change the bug behavior, but this can make the program run vastly slower and it’s just for debugging. Use git bisect or similar techniques to find the commit that introduced the bug. If the bug is introduced by a new / changed function call using an actual array argument that indexes into a non-contiguous array, that is a hint that the problem may be related to array temporaries. Enable compiler flags to help identify array temporaries in GFortran:

Suppose the following code snippet:

x = myfun(A(:, 4:16:3))

Here, A(:, 4:16:3) creates a non-contiguous array slice, leading to the creation of an array temporary when passed to myfun. Manually allocate an contiguous auxiliary array variable and copy the data into it before passing it to the function:

real, allocatable :: temp(:,:)

allocate(temp(size(A, 1), 5))
! Adjust size as needed

temp = A(:, 4:16:3)
! Copy data into contiguous array

x = myfun(temp)
! Pass the contiguous array

deallocate(temp)
! Clean up

Obviously this process is tedious, so we only employ it when needed to workaround a compiler bug.

How to use LaTeX directives

Many programming languages (including LaTeX) have directives or pragma–formatted comments that tells the compiler to do something specific.

LaTeX pragma: de facto directives are compatible with popular LaTeX editors including TeXstudio.

Specify LaTeX compiler and bibliography toolat the top of the main/root LaTeX document like:

% !TeX program = xelatex
% !BIB program = biber

LaTeX projects often use Biber for bibliography management. The above directives tell TeXstudio to use XeLaTeX compiler and Biber bibliography tool when compiling the document.

Trouleshooting missing XeTeX compiler

If XeTeX directive:

% !TeX program used: xelatex

yields:

Error: One command expansion invalid.
  Parent Command: compile
  Primary Command: compile

Fix by installing XeLaTeX compiler

  • Linux: apt install texlive-xetex
  • macOS: brew install mactex
  • Windows: get XeLaTeX via MikTeX

Restart TeXstudio to use XeLaTeX.

List MAC of Bluetooth devices on Windows

Find the MAC address of a Bluetooth device in Windows from Windows Device Manager under Bluetooth, (select device in list), Properties, Details, Bluetooth Device Address.

List all Bluetooth devices and their MAC addresses from PowerShell:

$devices = Get-PnpDevice | Where-Object { $_.Class -eq "Bluetooth" -and $_.FriendlyName }

$results = foreach ($device in $devices) {
    $id = $device.InstanceId
    $name = $device.FriendlyName

    $addressProp = Get-PnpDeviceProperty -InstanceId $id -ErrorAction SilentlyContinue |
                   Where-Object { $_.KeyName -like "*Address*" -and $_.Data -match '^\w{12}$' } |
                   Select-Object -First 1

    if ($addressProp) {
        $raw = $addressProp.Data.ToUpper()
        $bytes = for ($i = 0; $i -lt 12; $i += 2) {
            [convert]::ToByte($raw.Substring($i, 2), 16)
        }
        [array]::Reverse($bytes)
        $mac = ($bytes | ForEach-Object { $_.ToString('X2') }) -join ':'
        [PSCustomObject]@{ Device = $name; 'MAC Address' = $mac }
    }
}

$results | Format-Table -AutoSize

Cobra SoundTracker Compandor

Certain Cobra CB radios starting in the late 1990s including the Cobra 75 WX ST have a compandor circuit known as “SoundTracker” to reduce noise. The IC used for the SoundTracker included the TA31101AF, which is a generic audio compandor IC used in various radio communication devices. Compandoring compresses the dynamic range of an audio signal before transmission and expands it back to its original range upon reception, typically with a frequency vs. amplitude response curve tuned for the communication mode of the radio (say FM cordless phone vs. AM CB radio). Compandoring helps improve apparent audio SNR, but it can introduce artifacts, especially when only one side uses companding. Field experience indicates that SoundTracker can improve intelligibility for weak to moderate signals, including with non-SoundTracker radios, while adding a “huffy” character to recovered audio in some cases. Reported improvement can be up to about 20 dB when both sides enable SoundTracker, and up to about 10 dB when only one side enables it; artifacts are usually more noticeable in one-sided use. The digital noise reduction (DNR) circuit in the modern Cobra 75 All Road is generally superior to SoundTracker and avoids those specific compandor artifacts.

For the 2000s-era Cobra 75 WX ST, transmit modulation is high-level (yielding better sound quality and punchiness vs. low-level modulation of less expensive radios) and the radio includes an automatic noise limiter (ANL). Internal adjustments in the Cobra 75 WX ST allow optimizing transmit carrier power, modulation limiting, and other parameters. After two decades, the cord covering may crack and fall off, revealing the wires and antenna coax inside. In contrast to the modern Cobra 75 All Road, the 75 WX ST has all the RF hardware in the microphone, with the small gray box being an interface to the antenna, power, and speaker jack. The 75 WX ST is a good spare radio, but if purchasing a radio, consider instead a modern radio with DNR instead of SoundTracker, as the DNR is superior to the compandor.

TeXstudio and VS Code team editing

TeXstudio and VS Code are among the IDEs that support remote collaborative code / document editing with real-time updates. The program Teamtype is free, open-source, serverless, peer-to-peer, non-AI tech that enables no-cost real-time collaboration. 2 or more users connect via a Teamtype shared link code - no account or signup is used. Git can be used to capture specific versions or milestones of the LaTeX document. For general code editing and development with VS Code, Teamtype is also supported.

Overleaf has progressively added restrictions to its free plan, making TeXstudio (or VS Code)with Teamtype a good alternative for collaborative LaTeX editing. To have essential security features with an on-premises instantiation of open-source Overleaf Community Edition, a paid Overleaf Pro plan is required.


Alternatives:

  • server-based collaborative team editing is possible with VS Code Live Share.
  • Crixet has an appealing free plan, watch to see how the features evolve vs. pricing
  • Papeeria free version is quite restrictive, only 1 private project
  • Scienhub free version lacks Git integration and has limited storage
  • Authorea

AM two-way radio channel spacing

Since 1972, standard VHF AM used 25 kHz channel spacing generally worldwide. In 1999, European regulations began a mandatory phase-in of 8.33 kHz channel spacing for VHF AM aviation communications to increase the number of available channels. The rest of the world generally remains on 25 kHz spacing for aviation. One factor driving the 8.33 kHz spacing in Europe was the number of relatively close proximity airports and large number of small nations each having their own aviation communication needs. Other countries like China and the United States also have regions with many airports and a lot of air traffic, but each have a unified regulatory framework managing all VHF airband communications.

Anyone who has used AM CB radio or AM broadcast radio would understand why 25 kHz channel spacing is reasonable to avoid adjacent channel interference with real-world AM transmitters and receivers in most reasonable proximities. Even without overmodulated, splattering transmitters, an AM receiver can experience noticeable adjacent channel interference when within about 1 km of another AM transmitter operating on a channel just 10 kHz away. If the CB radio transmitter is operating properly, 20 kHz spacing is adequate when not closer than about 1 km.

This observation can be loosely derived from a typical AM CB radio (say the Cobra 19 mini) receiver’s selectivity performance of say 6 dB at 7 kHz spacing and 60 dB at 10 kHz spacing. Consider also the dynamic range of the receiver, akin to Sherwood Engineering’s Receiver test data, which doesn’t include CB radios. Assume the CB radio transmitter ERP is 36 dBm (4 watts) and the receiver sensitivity for 20 dB SINAD is -113 dBm (0.5 µV). At 1 km distance, free-space path loss at 27 MHz is about 61 dB. This first order approximation neglects important factors like radiation pattern distortion from ground reflections. Assuming 0 dB receiver antenna gain, received signal strength would be about -25 dBm, which can exceed the maximum dynamic range of the receiver front end, if we arbitrarily assume a receiver dynamic range of about 80 dB. However, the typical environmental noise floor on 27 MHz - neglecting skip or other anthropogenic interference - may be ~ -110 dBm in the AM receiver bandwidth, so the dynamic range impacts of a legal CB radio transmitter 1 km away may not be noticeable. An adjacent channel signal 10 kHz away (neglecting modulation bandwidth) would be attenuated by about 60 dB, resulting in a received adjacent channel signal strength of about -85 dBm. The adjacent channel signal would be strong enough to cause noticeable interference on the desired channel, and unwantedly open the squelch if no desired signal is present. With 20 kHz spacing, the adjacent channel signal would be attenuated by over 80 dB, approaching the general receiver’s dynamic range limit vs. the noise floor, and would be much less likely to cause noticeable interference, assuming the transmitter is clean.

The emissions mask in FCC part 95.979 requires better than -35 dBc in the band 8 kHz to 10 kHz from the carrier, and better than -59 dBc beyond 10 kHz from the carrier. The radio transmitter will often do better than -59 dBc at 20 kHz from the carrier and increasingly better beyond that.

Switching to AM VHF airband radio, a typical installed receiver such as the Icom IC-A220 has receiver selectivity at the 25 kHz step of 6 dB at 3 kHz spacing and 60 dB at 22 kHz spacing, and at the 8.33 kHz step of 6 dB at 2.8 kHz spacing and 60 dB at 7.4 kHz spacing. A typical handheld airband transceiver such as the Yaesu FTA-250L RX has selectivity in the 25 kHz step of 6dB at 8 kHz spacing and 60 dB at 25 kHz spacing, and at the 8.33 kHz step of 6dB at 2.8 kHz spacing and 60 dB at 8.3 kHz spacing. Thus we see how at airband, where the frequency plans would be made to slice up former 25 kHz step frequencies into distant frequency reuse at 8.33 kHz step, while avoiding close-by overload, the 8.33 kHz step is quite useful.

To put this back into CB radio terms, suppose CB radio is popular in a town. The typical use I’ve seen is for local groups to be at least 20 kHz apart to avoid adjacent channel interference. For example, CB channel 20 (27.205 MHz) is more popular as a “local” channel than channel 18 (27.175 MHz) to avoid adjacent channel interference to/from channel 19 (27.185 MHz). Since CB radio use by definition is entirely ad hoc, users generally space themselves 2 channels (20 kHz) apart in real-world observations. For airband VHF AM in Europe, channels of 8.33 kHz spacing are successfully used with proper frequency planning considering physical separation to avoid adjacent channel interference and overload.

Given the above, I think President should have make like 8 P-channels and not have had “P4” as CB channel 18. They should have instead done like P1 = CB1, P2 = CB3, P3 = CB12, P4 = CB14, P5 = CB22, P6 = CB23, P7 = CB27, P8 = CB29. This avoids CB channels 6, 9, 16 (SSB use), 19, and 20 and the SSB channels 30-40. However those are only hypothetical channel assignments, and President did not in fact do that. Given the existing President P-channel assignments, I think President should consider adding P6-P9 like: P6 = CB14, P7 = CB22, P8 = CB23, P9 = CB27. Again that’s hypothetical.