Scientific Computing

Matlab OpenCV C++/CUDA/MEX support

To use OpenCV from Matlab via vision Support Packages as integrated by the Mathworks using MEX.

Select “Computer Vision System Toolbox OpenCV Interface by MathWorks Computer Vision System Toolbox Team” and install.

Note: the examples require particular compilers depending on Matlab version and operating system.

Examples directory contains Computer Vision Toolbox examples from the Mathworks. Find the Matlab OpenCV example directory, in Matlab:

fileparts(which('mexOpenCV'))

The examples below assume you’re starting from this directory. See the README.txt in each directory for compilation details. Some examples require a CUDA GPU.

Foreground Detector: build example

cd ForegroundDetector

mexOpenCV backgroundSubtractorOCV.cpp

If the example fails to compile due to compiler mismatch, follow the instructions given in the error message.

Run the OpenCV Matlab demo:

testBackgroundSubtractor

You will see a Video Player window pop up with cars driving by, with the cars detected outlined in white rectangles.

print to stderr from Matlab / GNU Octave

In both Matlab and GNU Octave, functions like error and warning by default print messages to stderr in non-interactive sessions, for easier capture to uncluttered log files.

To print to stderr in general, use

fprintf(2, 'Hello text')

Verify these are printing to stderr by appending to the end of the command line command: 2>err.log

From GNU Octave:

octave-cli --eval "fprintf(2,'this is stderr')"

From Matlab:

matlab -batch "fprintf(2,'this is stderr')"

CMake builds for modern Fortran

Fortran languages standards keep long backward compatibility. Fortran 2018 finally deprecated Fortran 1958 fixed width format. In general across languages, compiler vendors take years to add the full language feature set. Automatically determining if a particular compiler supports a needed modern Fortran feature is straightforward with CMake (and Meson).

This CMake example is for error stop. Unlike C++, we do not typically need to enable modern Fortran features with specific compiler flags. Modern Fortran features are typically enabled by default.

include(CheckSourceCompiles)

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

check_source_compiles(Fortran "program test
error stop
end" f08errorstop)

if(f08errorstop)
  ...
endif()

Fortran submodule is supported by CMake.

Install Ruby Gems without sudo

Ensure Gems are installed to home directory under ~/gems, by adding to ~/.profile:

export GEM_HOME=$HOME/gems
export PATH=$PATH:$HOME/gems/bin

This allows installing Ruby Gems without sudo.

Windows laptop to remote Windows SSH + RDP

Assumes: Windows laptop connecting to a remote Windows PC with OpenSSH server and client built into Windows.

Remote PC IP Remote PC SSH port Remote PC RDP port
1.2.3.4 22 (open TCP firewall) 3389 (blocked by remote PC firewall)

Setup: on the Windows laptop, create script file sshrdp.bat:

start /b ssh -L 3391:localhost:3389 %1

@echo off
REM enough time to enter password
timeout /nobreak 10

mstsc /v:localhost:3391

Usage: from that directory, type sshrdp myhostname to connect RDP over SSH to myhostname computer that’s setup in your ~/.ssh/config file or via the IP address or hostname directly.

sshrdp.bat:

  1. makes the SSH connection (you’ll be prompted for SSH password, or use a public key file)
  2. makes the RDP connection over the SSH tunnel (where you will be prompted for the Windows password).

Notes

You can specify some Remote Desktop parameters on the MSTSC command line. There are many more options only available via saving an .RDP profile file.

You can create an .RDP file by saving a Remote Desktop Connection and then load that specific profile by:

mstsc "c:/MyPC.RDP" /v:localhost:3391
  • use Port 3391 for local forwarding because Windows uses Port 3390 for something else.

Related: Linux to Windows PC over SSH / RDP

Where is Python site-package directory

Python libraries are installed or linked to the lib/python?.?/site-packages/ directory. The location of this directory is revealed by:

python -c "import site; print(site.getsitepackages())"

The output will be like:

~/miniconda3/lib/python3.7/site-packages

There are multiple ways to setup virtual environments, the command above will be particular to the virtual environment, for example:

~/miniconda3/envs/py36/lib/python3.6/site-packages'

Modern Fortran submodule vs. preprocessor

Fortran has a long legacy of preprocessing systems, reaching back at least to the 1970s when C was not yet widely known. I would argue that modern Fortran ≥ 2008 has eliminated most preprocessors use cases. It’s usually much cleaner and quicker (build/runtime) to use Fortran submodule to switch functionality such as MKL vs. Atlas vs. Netlib Lapack using CMake, for example.

Numerous Python preprocessors have arisen (and sunk) along with the usual C/C++-based preprocessors. The only thing we use preprocessors for is very simple single-line statements, where adding a submodule would be significantly more cumbersome than a one-liner preprocessor statement.

Historically, very elaborate Fortran preprocessors were developed such that they nearly became a language unto themselves.

FLECS and Ratfor (also ratfiv, ratfor90, etc.)

These extensive preprocessors have been made obsolete by advances from Fortran 77 onward.

EZNEC antenna design tips

A few quick start tips for using EZNEC antenna design software for yagi antennas. This is assuming a yagi antenna for 440 MHz band.

  • Ground: start with “free space”. If the antenna model is too close to the ground, the simulation can be unrealistic. Once satisfied in free space, consider real ground (non-free space)
  • Number of segments: suggest 5 per wire, with free EZNEC license
  • Number of yagi elements: 3..5 elements are possible with free EZNEC at 440 MHz.
  • Source: 1 source, middle (50%) of “driven” wire

Example antennas

  • commercial yagi datasheets
  • coat-hanger yagi page 19 starting point 4 element yagi

Typical yagi performance

How good is good enough?

  • Gain: > 6 dBi
  • Backlobe (front/back ratio) (opposite direction of desired mainlobe): < -15 dB or so
  • Sidelobes (off side of desired direction): < -15 - -20 dB or so
  • SWR (439-441 MHz): Ideally less than 2.0. 3.0 is perhaps tolerable.

SWR is a measure of how efficient an antenna is at coupling energy to/from the air to a radio device.

Γ = (SWR - 1) / (SWR + 1)

  • Mismatch loss [dB] (energy loss due to SWR): -10 * (1 - Γ)^2

Example

Suppose: SWR = 3.0

  • Mismatch loss: -10 (1 - ((3-1) / (3+1))^2) = 1.25 dB

Power loss (gain) factors:

  • 3dB ~ factor of 2
  • 10dB ~ factor of 10
  • 20dB ~ factor of 100