Sunday, December 4, 2022

The Periodogram or Something Else?

The periodogram is the simplest and most common spectral estimator and, in R is implemented as

findPer <- function(x){(1/length(x))*abs(fft(x))^2},

but there are may othere spectral estimators*.

So, which spectral estimator should you pick? The flow chart below presents a reasonable way to pick between spectral estimators. The first question you should ask is, do you want to assume a model for the data? If you don’t know anything about the data or don’t trust what you know, then move to the left side of the flow chart. If the Fourier methods have enough resolution, use the periodogram or the Blackman-Tukey spectral estimator (BTSE). If they don’t, you could try the minimum variance spectral estimator (MVSE). If the MVSE does not have enough resolution, try a high order autoregressive (AR) estimate. The AR method does assume a model for the data, but a high enough AR model can estimate any PSD.

If you want to assume a model, we move to the right side of the flow chart. If we know the data consists of sinusoids in noise, then the multiple signal clarifier (MUSIC) estimator is a good choice. If the important features are peaks, but the data is not made up of sinusoids in noise, the AR estimator is a good choice. If nulls are the important features, then use a moving average (MA) estimator. If the important features are nulls and peaks, use the autoregressive moving average (ARMA) estimator. Finally, if you have no idea about the PSD shape and want to use a model, you could try a high order AR.

* For more info on spectral estimation, see my new book! -> Power Spectral Density Estimation by Example

Wednesday, November 30, 2022

Wednesday Linkorama

Round up of things I found interesting.

Space elevators have been a part of SiFi for a long time, this article talks about possibly building them for real! Space elevators are cables that go from the ground into space that a vehicle can use to climb into space. This article describes the basic ideas about how they work and why you may want to use one instead of rockets.

Crypto is starting to get the attention of regulators.

Diplomacy is a classic social board game focused on trickery and negotiations between players. The first games computers got really good at (like chess) required a lot of calculation. This article talks about Cicero, an AI that (allegedly) has human level performance in the game Diplomacy. So, computers may be getting better at other types of games.

Wednesday, November 2, 2022

Wednesday Linkorama

Round up of things I found interesting.

A Reddit user bought an old Netflix server and reconfigured it for personal storage.

New compression claims 10x over MP3. MP3 is already compressed so ... If this is 10x and sounds as good then they really have something.

Normally autonomous trucks are 5 years out, but Aurora says they are coming in 2024. So, maybe they are on to something?


Wednesday, October 26, 2022

Wednesday Linkorama

Round up of things I found interesting.

gsignal is an R package that is an improvement over the signal package. The signal package is a group of signal processing functions like you might find in Matlab. It was helpful but was missing some common functions, and some functions did not play well with complex numbers. The gsingal packaged added some key functions and works with complex numbers. It has the poly function but seems to be missing the roots function.

Ventura, the new mac operating system, is available. I'm going for it. Wish me luck!

Copilot is an app that helps write code. It uses what you wrote to suggest code snippets as you write the code. It looks interesting; I'm going to give it a try.

Tuesday, October 25, 2022

My new book is published!

This book provides the tools to use, understand, and apply power spectral density (PSD) estimation. By the end of this book, you will be able to select an appropriate PSD estimation method based on your data, perform the estimation, and understand the results. To accomplish this, we cover the methods and just enough theory so you can use the estimation techniques correctly and interpret the results meaningfully.

Get it here -> Spectral Density Estimation by Example

Thursday, August 27, 2020

Filtering Complex Data with R

Filtering

Base R has a function filter that can perform moving average (MA) or filters (AR). But it cannot do both at the same time; it cannot do an autoregresive ARMA filter. It also only works on real data. The filter function in the signal package can perform ARMA filters, but is is built on the filter function in the base package. So, it also cannot filter complex data. Below is an example of filtering some data with the filter from the signal package. We are using a 6 sample MA filter as specified by the b=rep(1/6,6).

N <- 64
x <- rnorm(N)
b <- rep(1/6,6)
a <- 1
y <- signal::filter(b,a,x)

The figure below has two plots. The blue circles are the Gaussian samples. The green circles are the filtered Gaussian samples. The filtered data has less variance.

Complex Data

In engineering the most common kind of complex data is the circularly symmetric gaussian noise. The circularly symmetric part means that the real and complex part are independent and identically distributed. I described the makeCG function in another post, but as you can see the real part and the imaginary part are made using separate calls to the rnorm function.

makeCG <- function(N,v=1) {(sqrt(v/2))*rnorm(N) + (sqrt(v/2))*1i*rnorm(N)}

The function filterComplex filters real or complex data. The function inputs the MA parameters b and the AR parameters a, and the data x. If the data is complex, it makes two filter calls: one with the real part and one with the imaginary part. If the data is real it make one call.

filterComplex <- function(b, a, x) {
  if (is.complex(x)) {
    y <- signal::filter(b,a,Re(x)) + 1i*signal::filter(b,a,Im(x))
  } else y <- signal::filter(b,a,x)
  return(y)
}

Here we simulate complex Gaussian data and use the function filterComplex to filter it.

N <- 64
x <- makeCG(N)
b <- rep(1/6,6)
a <- 1
y <- filterComplex(b,a,x)

The figure below has two plots. The blue circles are the real part of the complex Gaussian samples. The green circles are the real part of filtered complex Gaussian samples. As expected, the filtered data has less variance.

Now the imaginary part. The blue circles are the imaginary part of the complex Gaussian samples. The green circles are the imaginary part of filtered complex Gaussian samples.

Wednesday, June 24, 2020

Dungeons and Dragons: Advantage

D20 and Random Events

In the game Dungeons and Dragons, the success or failure of an event is determined by rolling a 20 sided die (D20): higher is better. If you need to roll 11 or higher you have a 50% chance of success. If another event requires 10 or better you now have a 55% chance for success. Each point the roll goes up or down is worth 5%. Often times, rolling a 20 is a critical success and a 1 is a critical failure; critical means it's extra good/bad.

Advantage

Sometimes things are really going your way and you roll with advantage. Sometimes things are not in your favor and you roll with disadvantage. When rolling with advantage, roll two dice and pick the higher. When rolling with disadvantage, roll two dice and pick the lower. How do advantage and disadvantage affect the chance of getting a 20 or 1?

When rolling without advantage or disadvantage, the probability of getting a 20 or a 1 is 1/20 = 0.05 or 5%. The probability of not getting a twenty 1 - 1/20 = 19/20.

P20    <- 0.05
Pnot20 <- 1-P20

When you have advantage, to not get a 20, you have to not roll a 20 twice. The probability of getting a 20 is (1 - the proability of not getting a 20 twice), and as you can see below, is almost 10%. Advantage about doubles you chance of getting a 20. But you probability guessed that since you're rolling twice. :) The chance of getting a 1 is 0.05^2=0.0025, almost 0!

Pnot20*Pnot20
## [1] 0.9025
1 - Pnot20*Pnot20
## [1] 0.0975

How will advantage and disadvantage affect the average roll? This time let's estimate the answer using a simulation. A 1000 trials will give a good estimate.

Trials <-10000

Below we simulate rolling two D20s

set.seed(1)
x <- sample(1:20,Trials,replace = TRUE)
y <- sample(1:20,Trials,replace = TRUE)

When we have advantage, roll two D20 and pick the max. With disadvantage roll two D20 and pick the min.

RollsWithAdvantage <- apply(cbind(x,y), 1,max)
Advantage    <-mean(RollsWithAdvantage)
RollsWithDisadvantage <- apply(cbind(x,y), 1,min)
Disadvantage <-mean(RollsWithDisadvantage)

Below is the simulated advantage mean, the calculated mean of a regular D20 roll, and the simulated disadvantage mean. Advantage adds about 3.4 points and disadvantage subtracts 3.4 or about +/- 17%.

Advantage
## [1] 13.8863
mean(1:20)
## [1] 10.5
Disadvantage
## [1] 7.1287

Below are 3 figures with histograms. The first histogram is made with 10,000 simulated D20 advantage rolls. Advantage moves a lot of probability to the right. The second histogram is made using 10,000 simulated D20 regular rolls. The histogram is approximately flat and each number is at about 0.05% and that's what we calculated. The last histogram is made with 10,000 simulated D20 disadvantage rolls. Disadvantage moves an equal amount to the left.

The probability of getting 11 or better with advantage is 1- probability of getting 10 or less twice. Rolling with advantage moves a 50% chance to a 75% chance!

p10orLess <- 0.5
1 - p10orLess^2
## [1] 0.75

Most Popular Posts