Tuesday, July 02, 2019

Textual Representation of SwiftUI Paths

While on vacation, I've been creating my first Swift Package, for my incomplete Scalar2D project. While my major ambitions have been slow, it does have 2 useful features: it supports many ways to represent colors as strings, and it can generate 2D paths from standard SVG path element strings.

So you can write:
import SwiftUI
import Scalar2D_SwiftUI
import CoreGraphics

public struct TestFrogView : View {
    private let cgPath = CGPath.path(fromSVGPath: "M185 212C139 183 40 199 39 108A18 18 0 1 1 56 75Q67 53 91 45A18 18 0 1 1 127 38C170 29 193 62 220 161L225 110Q231 84 260 115C260 142 265 205 241 198Q215 193 227 230C236 249 161 249 125 248A5 5 325 1 0 122 259C192 264 248 249 237 226Q230 206 247 211  266 210 272 161C273 139 276 106 252 93 245 86 209 65 216 133 200 46 176 19 132 26A28 28 0 0 0 81 40Q61 46 52 63A27 28 0 0 0 27 110C33 192 70 192 145 205Z"

    public var body: some View
           proxy in
            Path(self.cgPath.fitting(geometry: proxy)).strokedPath(StrokeStyle(lineWidth:3.0))
        }.frame(idealWidth:cgPath.boundingBoxOfPath.width, idealHeight:cgPath.boundingBoxOfPath.height)

Scalar2D_SwiftUI package in action

One thing, I found out after putting the package together was that the SwiftUI path has a String representable form of its own. You can pass it a specially formatted string and it will generate a Path struct.

Thus, an SVG Path to make a heart in a circle:
"M 0 0 A 100 100 0 0 0 0 200 A 100 100 0 0 0 0 0 Z M0 45 C 63-23 171 87 0 200 C -171 87 -63 -23 0 45 Z"
Simple SVG of a Heart in a Circle
Can be represented by a SwiftUI Path representation of:
0 0 m  -55.2285 0 -100 44.7715 -100 100 c -100 155.228 -55.2285 200 -1.83697e-14 200 c  55.2285 200 100 155.228 100 100 c 100 44.7715 55.2285 0 6.12323e-15 0 c h 0 45 m 63 -23 171 87 0 200 c -171 87 -63 -23 0 45 c h 
Which is probably just a direct translation of the underlying CGPath, which does not have an explicit arc element, so the arcs that made up my circle—the "A" operands—got converted to the "c"  cubic Bezier operands. The stringRepresentation property on Path tends to add unneeded short line segments after moves, which is also probably an artifact of iterating through the CGPath to generate the text. 

You could use the included Path(string: String) constructor to generate text from shapes, but SVG paths have the advantage of being generated by common drawing applications, and having more features such as arcs, shortcut operands and relative coordinates. I'm not sure why Apple didn't choose SVG paths when deciding on a text format, but please give my Scalar2D Swift Package a try. 

Monday, March 04, 2019

Optimizing a TV Antenna with an HDHomerun

I get asked by users of my Signal GH iOS app for the HDHomerun network TV tuner what measurement they are supposed to improve as they adjust their TV antenna systems.  

The HDHomerun provides 3 metrics describing the data stream coming from the over the air antenna: 
  1. Signal Strength
  2. Signal to Noise (or Signal Quality)
  3. Symbol Quality
I've been thinking about the analogy of someone with a noisy hearing aid trying to understand someone talking in a noisy room. From this way of thinking, Signal Strength is either turning up the volume or moving closer to the talker. It's how loud the voice is to the listener. The problem with this is, that the signal might be too noisy or maybe you are boosting as much noise as signal, or making it so loud that it's painful. Some gain might be useful, but just ramping up the Signal Strength has its limits. 

In this analogy, Signal Quality is how noisy the voice is when it gets to the listener, if the hearing aid is high quality, or the room is naturally quality, even a fairly quiet talker can be understand. You might try buying a better aid which can add gain without adding additional noise, or move closer to the speaker. 

Finally, Symbol Quality is a measure of did you understand all the words the speaker said. If not, your understanding can be severely distorted. When it comes to digital TV, you either understand everything perfectly or the picture pixelates. So, when it comes to Symbol Quality you want 100% perfection, nothing else will do. This is why I show it as either a green filled circle ⬤ or a red ring  ⭕️ and don't bother to show the Symbol Quality number.

As you can see, I'm prodding you to understand that while some Signal Strength is necessary, and Symbol Quality perfection is mandatory, it is long term Signal Quality that you want to improve. This is why Signal GH graphs Signal Quality over time, and not the other two metrics. If you can get the Signal Quality consistently above, let's say, 80, this will lead to getting perfect Symbol Quality  so your job as an antenna system optimizer is to find out what steps it will take. 

In my case, several years ago, I went through the process of getting bigger and bigger quality UHF only antennas, on taller masts, using a quality UHF pre-amp, and using my Signal GH app to find just the right direction to point the antenna—actually I used more primitive tools in the beginning and it's what led me to develop Signal GH. With each step, signal quality for each of my local stations increased, and each would eventually have such consistently high signal quality that the Symbol Quality would pretty much never dip below 100% perfection. Initially, I pulled in 2-3 stations good enough to watch without pixelation, and now I have 8-9 with dozens of sub channels, including every major. It's a rare—and usually blustery day—when my picture breaks up. I have an advantage in that all the stations I'm interested in are in the same general direction, or are close by, so I didn't have to resort to exotic antenna configurations: just bigger and taller. 

One thing I like about networked TV tuners, like the HDHomerun, or the Tablo, is that since they are shared resources, I don't have to split my signal to every room in the house, instead, I split my antenna signal between an HDHomerun for live TV, and running my app; a Tablo for recording scheduled content; and my main TV for watching major sporting events, i.e. pro football. So, only split three ways. With a 3 way splitter, one of the splits gets twice the signal of the other two, and this output I connect to the Tablo, as it will be recording unattended. Minimizing splits is one way to get better signal quality. 

Another signal improving quality of networked tuners is shorter cable runs, as they can installed right next to the splitter. 

Here's a video I made to elaborate on signal metrics:

By the way, I occasionally hear from other Tablo users who own an HDHomerun solely to run Signal GH. Gratifying but I wish I could support the Tablo, but they don't appear interested in providing the data I would need.