# coherence_ms (MathScript RT Module Function)

LabVIEW 2012 MathScript RT Module Help

Edition Date: June 2012

Part Number: 373123C-01

»View Product Info Download Help (Windows Only)

Owning Class: spectral analysis

Requires: MathScript RT Module

## Syntax

coherence_ms(x, y)

coherence_ms(x, y, win)

coherence_ms(x, y, win, noverlap)

coherence_ms(x, y, win, noverlap, fftsize)

coherence_ms(x, y, win, noverlap, range)

coherence_ms(x, y, win, noverlap, fftsize, fs)

coherence_ms(x, y, win, noverlap, fftsize, range)

coherence_ms(x, y, win, noverlap, fftsize, fs, range)

Coxy = coherence_ms(x, y)

Coxy = coherence_ms(x, y, win)

Coxy = coherence_ms(x, y, win, noverlap)

Coxy = coherence_ms(x, y, win, noverlap, fftsize)

Coxy = coherence_ms(x, y, win, noverlap, range)

Coxy = coherence_ms(x, y, win, noverlap, fftsize, fs)

Coxy = coherence_ms(x, y, win, noverlap, fftsize, range)

Coxy = coherence_ms(x, y, win, noverlap, fftsize, fs, range)

[Coxy, w] = coherence_ms(x, y)

[Coxy, w] = coherence_ms(x, y, win)

[Coxy, w] = coherence_ms(x, y, win, noverlap)

[Coxy, w] = coherence_ms(x, y, win, noverlap, fftsize)

[Coxy, w] = coherence_ms(x, y, win, noverlap, range)

[Coxy, w] = coherence_ms(x, y, win, noverlap, fftsize, range)

[Coxy, f] = coherence_ms(x, y, win, noverlap, fftsize, fs)

[Coxy, f] = coherence_ms(x, y, win, noverlap, fftsize, fs, range)

Legacy Name: `mscohere`

## Description

Calculates the magnitude-squared coherence estimation of two signals. If you do not request an output, this function plots the coherence in the current plot window.

Details

Examples

## Inputs

Name Description
x Specifies the first input sequence. x is a real or complex vector.
y Specifies the second input sequence. y is a real or complex vector. length(x) must equal length(y).
win Specifies the window to apply to each segment of x and y. The length of win determines the number of segments into which LabVIEW divides x and y. win can be a vector that represents the window coefficients or an integer that represents a Hamming window with a length of win. The default is a Hamming window where LabVIEW divides x and y into eight sections.
noverlap Specifies the number of data elements that overlap between adjacent segments of data. noverlap must be less than the length of win. The default is a length that results in an overlap of half of the data elements between adjacent segments.
fftsize Specifies the length of the FFT to perform on each segment of the data. The default is the next power of 2 greater than the length of each segment of x and y if this number is greater than 256. Otherwise, the default is 256.
fs Specifies the sampling frequency of the input sequences in Hz. If you specify fs, LabVIEW returns the output frequencies in Hz. If you do not specify fs, LabVIEW returns the output frequencies in radians per sample.
range Specifies the range of the estimated magnitude-squared coherence. range is a string that accepts the following values.

 'onesided' (default) LabVIEW returns the single-sided estimated magnitude squared coherence. 'twosided' LabVIEW returns the double-sided estimated magnitude squared coherence.

## Outputs

Name Description
Coxy Returns the magnitude-squared coherence of the two input signals x and y. If both x and y are real and range is 'onesided', the length of Coxy is fftsize/2 + 1 for an even fftsize and (fftsize + 1) / 2 for an odd fftsize. Otherwise, the length of Coxy is fftsize.
w Returns the frequencies in radians per sample at the points where LabVIEW evaluates Cxy.
f Returns the frequencies in Hz at the points where LabVIEW evaluates Cxy.

## Details

This function uses the Welch method to calculate the power spectrum of x, the power spectrum of y, and the cross power spectrum of x and y. LabVIEW uses the following equation to calculate the magnitude-squared coherence estimation:

Coxy(f) = |P_xy(f)|^2/(P_xx(f)*P_yy(f))

The following table lists the support characteristics of this function.

 Supported in the LabVIEW Run-Time Engine Yes (if you request output) Supported on RT targets Yes (if you request output) Suitable for bounded execution times on RT Not characterized

## Examples

fs = 1000;
t = 0:1/fs:0.2;
fftsize = 1024;
window = win_hann2(64);
noverlap = 32;
noise = randnormal(size(t));

x = sin(2*pi*100*t) + cos(2*pi*250*t) + noise;
y = 2*sin(2*pi*50*t) + 3*cos(2*pi*400*t) - noise;
Coxy = coherence_ms(x, y, window, noverlap, fftsize, fs);