Electronics, MATLAB, SDR

Magnetic Communications

While playing with the magnetometer sensor on the Arduino for my DIY smart bike project I had an idea that I might be able to use a magnetometer as a crude communications receiver. The question being, Can we transmit data, by modulating a magnetic field, to a computer using a magnetometer sensor as a receiver, and recover that information correctly?

Communication Basics

For this, we need to go back to basics! How can we send information from one device to another so that it is received correctly? This simple question forms the basis of a huge field of study called Information Theory. This field studies the transmission, processing, extraction, and utilization of information. It was first developed by Claude Shannon in his foundational work “A Mathematical Theory of Communication” where he lays out the basic building blocks of digital communications.


“A Mathematical Theory of Communication” by C. E. Shannon

Receiver

This whole chain needs to be taken into account, but I will start at the receiver since it is the main bottleneck in my magnetic communications system. A receiver is characterized by many different physical forces for a specific channel, but for the digital domain, two simple characteristics determine the amount of information that the receiver can recover and these are sampling rate and the dynamic range of those samples. On an ADC (Analog to Digital Converter) these are known as Samples per Second and bit precision. An example could be a 1MSps 8bit ADC, where an analog signal is sampled 1 Million times per second with 256 different quantization levels.

The sampling rate is important because of how it relates to the Nyquist Theorem which posits that a bandwidth-limited signal needs to be sampled at two times its highest bandwidth frequency in order to prevent aliasing of the signal. But due to physical limitations and to improve a receiver’s chance of correctly recovering the data the sampling rate needs to usually be greater than 2 times the bandwidth. PySDR Sampling has a good description with graphs on how sampling under the Nyquist rate causes aliasing of the signal.

The bit depth or resolution of an ADC is also important as it defines how precise it can measure the analog signal effectively giving you a better representation of the signal. But only up to a point there is diminishing gain to adding more bits. This is because the noise in the system can make those extra bits of precision worthless. This is called Effective Number of Bits ENOB and it is based on the received SNR of the signal.

Channel

this leads us to the channel, this is the physical medium used for transmitting your data from one entity to another. Conventionally this could be copper wire, radio wave, optical fiber, infrared light, etc. all channels have noise or random variations in the measurement of a signal represented in them. The amount of noise relative to our signal is known as SNR. Larger SNR means that our receiver is receiving more of our desired signal than the background interference. If the SNR is too low we will not be able to correctly recover the originally sent data.

Physical channels also have other effects that can impact data transmissions such as fading and multipath in radio waves, chromatic dispersion in the fiber, or other distortions.

Transmitter

The transmitter is the device that converts a message or data into a physical form that can be sent over the intended channel. An example of this could be a laser that emits light pulses over a fiber optic cable or an antenna that emits radio waves over the air. All of these are driven by electrical signals that are modulated with the data.

Destination Recovery

Once the receiver has sampled the signal sent over the channel by the transmitter we must work out a way to interpret the data correctly. This starts with making sure we are sampling the transmitted symbol in the correct place since the transmitter and receiver are not synchronized by the same clock. We can then employ other packet-based methods such as using a preamble to denote the start of a transmission, a simple CRC check, or more complicated error correction schemes.

Error checking and correction are especially important in one-way transmission since there is no way for the receiver to report to the transmitter that it needs to send the data again. This one-way data transmission problem was highlighted in a competition to create robust and reliable communications called The Radio Resilience Competition.

Main Idea

Now after that “brief” detour in digital communications basics we can get back to mapping those concepts to a magnetic field communication channel. As mentioned in the introduction I was curious if a magnetometer could be used as a receiver, that just left the transmitter. For this, the easiest way to control a magnetic field is with an electromagnet, where we can modulate an electric current in a coil to send the data across the channel to the magnetometer receiver.

Hardware Prototype

Now that I had the basic idea worked out I decided to build a hardware prototype so I could test the communication algorithms in a real-world system.

For the receiver, I used an old iPhone 7 and the PhyPhox app to record magnetometer data. Thanks to the many technological advancements in cellular and sensor technology every smartphone on the market comes bundled with a magnetometer sensor for aiding in navigation.

And the app allows me to record the raw magnetometer readings to a CSV file for off-device processing. The iPhone looks like it samples at a maximum of 100sps but it was hard to find specific specifications. I decided against using the Arduino as it was harder to interact with and I ultimately want to use a phone as the final receiver target.

The transmitter is a bit more complicated, I needed a way of modulating a magnetic field with an electrical current, thankfully these physical forces are very related.

  • An electromagnet is a type of magnet where the magnetic field is produced by an electric current through a piece of coiled wire, usually wound around a core of ferromagnetic material such as iron.

I started with a simple piece of wire coiled in a circle and connected it up to a USB waveform generator (Analog Discovery), but it quickly became apparent that the heavy wire would not react fast enough to provide faster communication speeds. This is when I remembered an interesting project by Carl Bugeja who created the FlexAR, a small flexible PCB that was meant for small electromagnetic actuator projects. This worked considerably better and allowed me to support another Maker creating some really fun projects.

Transmitter Waveform Design

A transmitting waveform design needs characteristics that take advantage of the channel medium that it will cross, and is tightly coupled to the capabilities of the receiver. Using the Analog Discovery USB arbitrary waveform generator in the Waveforms Software I was able to create a variety of waveform designs that could fit the medium.

Basic NRZ

The first thing that I tried was a simple NRZ or Non-Return-to-Zero waveform that encodes 1s as +1 and 0s as -1s. With this pattern, I set the transmitter on the iPhone, using the plotting function of the app to determine visually where the best location was. I then scaled the frequency and voltage output of the waveform generator to drive a signal that I could easily make out visually on the graph. This symbol frequency is extremely low compared to other communication systems in the range of a few bits per second.

M-PAM

Due to the low symbol rate, I believed I could increase the data transmission by encoding more bits per symbol. This means there need to be more logical levels in the waveform, this can be achieved by using a modulation scheme called Pulse Amplitude Modulation PAM. Common levels of PAM are 2 and 4 that are used in many high-speed serial communications. I was able to create waveforms for 4 and even 8 levels encoding 2 and 3 bits per symbol respectively. But the issue that I arrived at was that it was hard to recover the level correctly in post-processing. I’m not sure if this has to do with the precision of the magnetometer or how it is being sampled in software.

Pulse Shaping

To try to improve the reliability of capturing the symbol timing I added some crude pulse shaping by adding a peak to each symbol in the NRZ waveform which allowed me to improved symbol timing recovery even when there were long runs of a single level.

Receiver Processing

For the receiver processing, I decided to record the raw data using the app to s simple CSV file that I could import to Python or MATLAB. This allowed me to save time in the proof of concept since I did not have to write my own app to interface to the magnetometer sensor. I decided to use MATLAB since I have it but all of this processing could be done in Python with a bit more effort, and I will need to translate the processing at some point for on-device app processing.

My first challenge was to read in the recorded data into MATLAB, thankfully it has a built-in function that allows you to create a script to read all the data in the CSV into a table that can then be converted into an array.

Generate a script by selecting the “Import Selection” dropdown then click “Generate Script”

Once I had the data in MATLAB I could start trying to recover the correct symbols out of the sampled data. This was done in conjunction with the waveform development as I kept updating the waveform as necessary to get a better processing result. So this will show a similar process to the waveform design section from the perspective of the receiver.

M-PAM

First, we need to just plot the received signal to see what we are dealing with. From there we can start to devise a method for recovering the symbols that were sent.

To analyze the performance of our processing we will use several graphs that will allow us to see well we are differentiating the symbols. The eye diagram shows us the transition between one symbol to another and the constellation diagram shows how separated individual sampled symbols are. I first tried the built-in MATLAB symbol recovery function but was having trouble getting the output to match.

I believe this to be because M-PAM might not be covered by the built-in algorithms since on further reading higher level pam needs symmetric transitions for symbol timing recovery. Therefore I devised my own crude method using some of my signal processing and math background knowledge.

My symbol timing recovery method consisted of several stages of signal processing which are outlined below:

Matched filter -> Derivative -> Sign function -> Zero crossing -> Decimate est. -> Output

The matched filter is basically a simple square pulse that has the same number of samples as the transmitted symbol.

When we filter the raw data with the matched filter it creates peaks where we should be able to sample the symbol correctly. We can then take the derivative of that filtered output to locate the sample peaks.

We can then use the sign function on the derivative signal to accentuate positive to negative changes. A simple loop can then denote where that derivative crosses zero indicating where we should sample the symbol.

Since our transmitter is sending a repeating pattern we can visually check to see if our processing output matches the sent data. Then we can use the eye and installation diagrams to show how distinct the sampled symbols are.

My algorithm recovery left vs matlab function output on the right.

I was able to get passable results with this modulation and recovery method but I believe that it is currently too complicated to develop for now. I will revisit it in the future once a simpler case is more proven.

NRZ with Pulse Shaping

As with the M-PAM, we first plot our received signal as well as its raw eye diagram and constellation to see what we are dealing with.

The Matlab function still failed to resolve an accurate symbol timing so I decided to try the same method that showed progress in the other modulation

Building off the basic algorithm used for the M-PAM modulation symbol recovery, we can more easily recover the symbols in the NRZ with pulse shaping waveform. This is because the peaks built into the transmitted waveform are better accentuated by the matched filter. This means the derivative can detect the peak of a symbol easier especially for multiple same symbols in a row.

Matched filter -> Derivative -> Sign function -> Zero crossing -> Decimate est. -> Output

Again the matched filter is a centered pulse with the length equal to the transmitted symbol sample rate.

The raw magnetic data is normalized and zero centered before the matched filter is run over the data. This creates a symmetric signal where the shaped pulses are much more apparent in the filtered signal. The derivative of the filtered signal is shown in orange.

The sign of the derivative signal denotes changes from positive to negative and will help us locate where we should sample the symbols correctly.

A simple loop is used to pull out the location of the transitions and the recovered symbol. The loop uses a simple delay filter to ignore transitions that would be too close to the previous symbol. This allows for correct decimation of the signal to the desired data symbols.

Using the Eye and constellation diagrams again to measure the variability of the signal visually, we can see a very well-defined Eye and distinct constellation points with a very small spread.

We can also match up the recovered symbols to the transmitted symbols to see if there are any errors in sampling. The second graph looks like it only has one signal but that is because the recovered and the actual are overlapping perfectly.

Conclusion and Next Steps

From this the ability to send data over a modulated magnetic field is possible but the low data rate is the limiting factor. So what are some possible uses for this technology? From where I see it it has some very useful properties one being extreme near field communications for a security side channel, and almost every smartphone has a magnetometer already built-in, meaning no extra hardware is required. The transmission only needs a simple electromagnet which can be printed on a low-cost PCB manufacturing process.

In my next steps, I need to formalize the processing and add a simple way of sending arbitrary data over the transmitter. I also need to add error correction and packetization to the design. I will then investigate other hardware that could be used to create the waveform instead of the expensive USB device. There are still gains to be had in investigating other waveform designs that could push the data symbol rate higher.

The sampling period of .01s indicates an average sample rate of 100sps, this means that the current samples per symbol of 10 are conservative, and we should be able to push that down by at least half with more improvements. Although I suspect that the ability of the magnetic field to change more rapidly could be the physical limitation of the prototype hardware.

Educational Benefit

From my explorations in creating this technology, I believe that it could have a massive benefit to university students learning signal processing and communications as it allows for over-the-air data transmission without the need for expensive software-defined radios and complex software installations. While the Analog discovery is expensive they do offer educational discounts and would be useful for other coursework as well. This is especially true while COVID keeps students away from university lab equipment.

Code

For those that have made it this far I have uploaded the Waveforms workspace files along with the Matlab code used to process them. You will have to change the file path to match your system. Have fun and happy hacking!

GitHub: Prototype-Magnetic-Comms