测量信号相似性

比较具有不同采样率的信号

```load relatedsig figure ax(1) = subplot(3,1,1); plot((0:numel(T1)-1)/Fs1,T1,"k") ylabel("Template 1") grid on ax(2) = subplot(3,1,2); plot((0:numel(T2)-1)/Fs2,T2,"r") ylabel("Template 2") grid on ax(3) = subplot(3,1,3); plot((0:numel(S)-1)/Fs,S) ylabel("Signal") grid on xlabel("Time (s)") linkaxes(ax(1:3),"x") axis([0 1.61 -4 4])```

`[Fs1 Fs2 Fs]`
```ans = 1×3 4096 4096 8192 ```

```[P1,Q1] = rat(Fs/Fs1); % Rational fraction approximation [P2,Q2] = rat(Fs/Fs2); % Rational fraction approximation T1 = resample(T1,P1,Q1); % Change sample rate by rational factor T2 = resample(T2,P2,Q2); % Change sample rate by rational factor```

在测量值中查找信号

```[C1,lag1] = xcorr(T1,S); [C2,lag2] = xcorr(T2,S); figure ax(1) = subplot(2,1,1); plot(lag1/Fs,C1,"k") ylabel("Amplitude") grid on title("Cross-Correlation Between Template 1 and Signal") ax(2) = subplot(2,1,2); plot(lag2/Fs,C2,"r") ylabel("Amplitude") grid on title("Cross-Correlation Between Template 2 and Signal") xlabel("Time(s)") axis(ax(1:2),[-1.5 1.5 -700 700])```

```[~,I] = max(abs(C2)); SampleDiff = lag2(I)```
```SampleDiff = 499 ```
`timeDiff = SampleDiff/Fs`
```timeDiff = 0.0609 ```

测量信号之间的延迟并将它们对齐

```figure ax(1) = subplot(3,1,1); plot(s1) ylabel("s1") grid on ax(2) = subplot(3,1,2); plot(s2,"k") ylabel("s2") grid on ax(3) = subplot(3,1,3); plot(s3,"r") ylabel("s3") grid on xlabel("Samples") linkaxes(ax,"xy")```

`t21 = finddelay(s1,s2)`
```t21 = -350 ```
`t31 = finddelay(s1,s3)`
```t31 = 150 ```

`t21` 表示 `s2``s1` 滞后 350 个采样，而 `t31` 表示 `s3``s1` 超前 150 个采样。现在可使用此信息通过时移信号来对齐这 3 个信号。我们还可以使用 `alignsignals` 函数，通过延迟最早的信号来对齐这些信号。

```s1 = alignsignals(s1,s3); s2 = alignsignals(s2,s3); figure ax(1) = subplot(3,1,1); plot(s1) grid on title("s1") axis tight ax(2) = subplot(3,1,2); plot(s2) grid on title("s2") axis tight ax(3) = subplot(3,1,3); plot(s3) grid on title("s3") axis tight linkaxes(ax,"xy")```

比较信号的频率成分

```Fs = FsSig; % Sample Rate [P1,f1] = periodogram(sig1,[],[],Fs,"power"); [P2,f2] = periodogram(sig2,[],[],Fs,"power"); figure t = (0:numel(sig1)-1)/Fs; subplot(2,2,1) plot(t,sig1,"k") ylabel("s1") grid on title("Time Series") subplot(2,2,3) plot(t,sig2) ylabel("s2") grid on xlabel("Time (s)") subplot(2,2,2) plot(f1,P1,"k") ylabel("P1") grid on axis tight title("Power Spectrum") subplot(2,2,4) plot(f2,P2) ylabel("P2") grid on axis tight xlabel("Frequency (Hz)")```

`mscohere` 函数计算两个信号之间的频谱相干性。该函数确认 `sig1``sig2` 在 35 Hz 和 165 Hz 附近有两个相关分量。在频谱相干性高的频率中，相关分量之间的相对相位可以用交叉频谱相位来估计。

```[Cxy,f] = mscohere(sig1,sig2,[],[],[],Fs); Pxy = cpsd(sig1,sig2,[],[],[],Fs); phase = -angle(Pxy)/pi*180; [pks,locs] = findpeaks(Cxy,MinPeakHeight=0.75); figure subplot(2,1,1) plot(f,Cxy) title("Coherence Estimate") grid on hgca = gca; hgca.XTick = f(locs); hgca.YTick = 0.75; axis([0 200 0 1]) subplot(2,1,2) plot(f,phase) title("Cross-Spectrum Phase (deg)") grid on hgca = gca; hgca.XTick = f(locs); hgca.YTick = round(phase(locs)); xlabel("Frequency (Hz)") axis([0 200 -180 180])```

35 Hz 分量之间的相位滞后接近 -90 度，165 Hz 分量之间的相位滞后接近 -60 度。

求信号的周期性

```load officetemp.mat Fs = 1/(60*30); % Sample rate is 1 sample every 30 minutes days = (0:length(temp)-1)/(Fs*60*60*24); figure plot(days,temp) title("Temperature Data") xlabel("Time (days)") ylabel("Temperature (Fahrenheit)") grid on```

```maxlags = numel(temp)*0.5; [xc,lag] = xcov(temp,maxlags); [~,df] = findpeaks(xc,MinPeakDistance=5*2*24); [~,mf] = findpeaks(xc); figure plot(lag/(2*24),xc,"k",... lag(df)/(2*24),xc(df),"kv",MarkerFaceColor="r") grid on xlim([-15 15]) xlabel("Time (days)") title("Auto-Covariance")```

```cycle1 = diff(df)/(2*24); cycle2 = diff(mf)/(2*24); subplot(2,1,1) plot(cycle1) ylabel("Days") grid on title("Dominant Peak Distance") subplot(2,1,2) plot(cycle2,"r") ylabel("Days") grid on title("Minor Peak Distance")```

`mean(cycle1)`
```ans = 7 ```
`mean(cycle2)`
```ans = 1 ```