Engee documentation

meanfreq

The average frequency.

Library

EngeeDSP

Syntax

Function call

  • freq, power = meanfreq(x) — evaluates the average normalized frequency freq a signal in the time domain x. It also returns the spectral power power in a given frequency band. To calculate the spectral power meanfreq uses the function periodogram with a rectangular window and the number of DFT points equal to the length x.

  • freq, power = meanfreq(x,Fs) — evaluates the average frequency in units of sampling frequency Fs.

  • freq, power = meanfreq(pxx,f) — returns the average frequency of the power spectral density estimation pxx. Frequencies f correspond to the estimates in pxx.

  • freq, power = meanfreq(sxx,f,rbw) — returns the average frequency of the power spectrum estimation sxx with a resolution band rbw.

  • freq, power = meanfreq(_,freqRange) — also uses the frequency range freqRange, on which the average frequency is calculated. This syntax can include any combination of input arguments from the previous syntaxes, provided that the second input argument is either Fs or f. If the second input argument is passed empty, then meanfreq uses a normalized frequency. The default value for freqRange — the entire bandwidth of the input signal.

  • meanfreq(_,out=:plot) — Plots the power spectral density or power spectrum and annotates the average frequency.

Arguments

Input arguments

# x — input signal

+ vector | the matrix

Details

An input signal specified as a vector or matrix. If x — vector, then meanfreq processes it as a single channel. If x is a matrix, then the function calculates the average frequency independently for each column x.

Типы данных

Float32, Float64

# Fs — sampling rate
positive real scalar

Details

The sampling frequency, set as a positive real scalar. The sampling rate is the number of samples per unit of time. If the unit of time is seconds, then the sampling frequency is indicated in Hz.

Типы данных

Float32, Float64

# pxx — spectral power density

+ vector | the matrix

Details

The spectral power density, defined as a vector or matrix. If pxx — the matrix, then meanfreq calculates the average frequency independently for each column pxx.

The power spectral density should be expressed in linear units, not in dB.

Типы данных

Float32, Float64

# f — frequencies

+ vector

Details

Frequencies specified as a vector.

Типы данных

Float32, Float64

# sxx — spectral power estimation

+ vector | the matrix

Details

Spectral power estimate, specified as a vector or matrix. If sxx — the matrix, then meanfreq calculates the average frequency independently for each column sxx.

The power spectral density should be expressed in linear units, not in dB.

Типы данных

Float32, Float64

# rbw — frequency resolution

+ positive scalar

Details

Frequency resolution, set as a positive scalar. The frequency resolution is defined as the product of two quantities: the frequency resolution of the discrete Fourier transform and the equivalent noise bandwidth of the window used to calculate the power spectral density.

Типы данных

Float32, Float64

# freqRange — frequency range

+ two-element vector

Details

The frequency range specified as a two-element vector of real values. If freqRange not specified, then meanfreq uses the full bandwidth of the input signal.

Типы данных

Float32, Float64

Output arguments

# freq — average frequency

+ scalar | vector

Details

The average frequency returned as a scalar or vector.

  • If you specify the sampling rate, then freq it has the same units of measurement as Fs.

  • If the sampling frequency is not specified, then freq it has units of measurement of rad/count.

# power — spectral power in the frequency band

+ scalar | vector

Details

The spectral power in the frequency band, returned as a scalar or vector.

Examples

Average chirp frequency

Details

Generate 1024 sampling rate of the chirp signal 1024 kHz. Let’s set the chirp frequency so that it starts with 50 kHz and reached 100 kHz at the end of the signal. Add white Gaussian noise so that the signal-to-noise ratio is 40 dB. Restart the random number generator to get reproducible results.

import EngeeDSP.Functions: meanfreq, chirp, std
using Random

nSamp = 1024
Fs = 1024e3
SNR = 40
Random.seed!(0)
t = (0:nSamp-1)./ Fs
x = chirp(t, 50e3, nSamp/Fs, 100e3)
x = x.+ randn(nSamp).* std(x)[1]./(10^(SNR/20))

Let’s estimate the average frequency of the chirp signal. Let’s plot the power spectral density and determine the average frequency.

meanfreq(x,Fs,out=:plot)

meanfreq 1

We will generate another chirp signal. Setting the initial frequency 200 kHz, the final frequency 300 kHz and an amplitude twice the amplitude of the first signal. Add white Gaussian noise.

x2 = 2*chirp(t,200e3,nSamp/Fs,300e3)
x2 = x2.+ randn(nSamp).* std(x2)[1]./(10^(SNR/20))

Combine the chirp signals to produce a two-channel signal. Let’s estimate the average frequency of each channel.

y = meanfreq([x x2],Fs)
([75048.0093015613 250002.6501495102], [0.49966449511918404 2.0006502475894763])

Let’s plot the spectral power density of the two channels.

y = meanfreq([x x2],Fs,out=:plot)

meanfreq 2

Add up the two channels to form a new signal. Let’s plot the power spectral density and determine the average frequency.

y = meanfreq(x+x2,Fs,out=:plot)

meanfreq 3

The average frequency of the sine wave

Details

Generate 1024 sampling of a sinusoidal signal with a frequency 100.123 kHz, sampled with frequency 1024 kHz. Add white Gaussian noise so that the signal-to-noise ratio is 40 dB. Restart the random number generator to get reproducible results.

import EngeeDSP.Functions: meanfreq, sin, std
using Random

nSamp = 1024
Fs = 1024e3
SNR = 40
Random.seed!(0)
t = (0:nSamp-1)./ Fs
x = sin.(2pi * t * 100.123e3)
x = x.+ randn(nSamp).* std(x)[1]./(10^(SNR/20))

Let’s estimate the average frequency of the sinusoidal signal. Let’s plot the power spectral density and determine the average frequency.

meanfreq(x,Fs,out=:plot)

meanfreq 4

We will generate another sinusoidal signal with a frequency of 257.321 kHz and an amplitude twice the amplitude of the first sine wave. Add white noise.

x2 = sin.(2pi * t * 257.321e3)
x2 = x2.+ randn(nSamp).* std(x2)[1]./(10^(SNR/20))

Combine the sinusoids to produce a two-channel signal. Let’s estimate the average frequency of each channel.

y = meanfreq([x x2],Fs)
([100127.71993300629 257421.94010929883], [0.4996432155771679 0.4994390782715168])

Let’s plot the spectral power density of the two channels.

y = meanfreq([x x2],Fs,out=:plot)

meanfreq 5

Add up the two channels to form a new signal. Let’s plot the power spectral density and determine the average frequency.

y = meanfreq(x+x2,Fs,out=:plot)

meanfreq 6

Average frequency of band-limited signals

Details

Let’s create a signal whose spectral power density resembles the frequency response of a bandpass FIR filter. 88-th order with normalized cutoff frequencies.

import EngeeDSP.Functions: fir1, meanfreq, bandpower

d = fir1(88,[0.25 0.45])

meanfreq(d,[],[0.3 0.6]*pi, out=:plot)

meanfreq 7

We will output the average frequency and power for the measurement interval. Specifying the sampling rate it is equivalent to the fact that the sampling frequency is not set.

mf=meanfreq(d,[],[0.3 0.6]*pi)
println("Mean = $(round(mf[1]/pi, digits=3))*pi, power = $(round(mf[2]/bandpower(d)*100, digits=1))% of total")
Mean = 0.373*π, power = 75.6% of total

Add a second channel with normalized cutoff frequencies 0.5π rad/countdown and 0.8π rad/count and an amplitude of one tenth of the amplitude of the first channel.

d2=fir1(88,[0.5 0.8])/10
meanfreq([d d2],[],[0.3 0.9]*pi, out=:plot)

meanfreq 8

Let’s output the average frequency of each channel. Divide by π.

mf=meanfreq([d d2],[],[0.3 0.9]*pi)
println(mf[1]/pi)
[0.3730065696360713 0.6500002992402633]

Algorithms

Details

To determine the average frequency meanfreq calculates an estimate of the periodogram’s power spectrum using a rectangular window.

The same value of the average frequency mFrq can be obtained from the signal x with sampling rate Fs in three ways.

Directly from the signal

mFrq = meanfreq(x,Fs)

According to the periodogram of the signal

P,F = periodogram(x,[],length(x),Fs); mFrq = meanfreq(P,F)

Based on an estimate of the spectral power of the signal (spectral Welch power density)

P,F = pwelch(x,rectwin(length(x)),[],length(x),Fs); mFrq = meanfreq(P,F)

Because meanfreq It uses an intermediate representation to convert the input signal from the time domain to the frequency domain. The returned average frequency may vary depending on the signal conversion method, the number of DFT points, and the window size.

Literature

  1. Phinyomark, Angkoon, Sirinee Thongpanja, Huosheng Hu, Pornchai Phukpattaranont, and Chusak Limsakul, The Usefulness of Mean and Median Frequencies in Electromyography Analysis, Computational Intelligence in Electromyography Analysis — A Perspective on Current Applications and Future Challenges, edited by Ganesh R. Naik. London: IntechOpen, 2012. https://doi.org/10.5772/50639.