Normalizing Sampling Frequencies
In signal processing appplications, it is frequently necessary to compare data sets that possess different sampling rates or durations. For instance, matching a live audio stream against a database of stored templates requires normalization, as stored data is often downsampled to conserve memory. Direct subtraction or analysis is impossible without consistent time bases. The solution involves resampling the lower-frequency signals to match the higher-frequency target using a rational conversion factor, wich effectively applies an anti-aliasing filter.
% Load the signal data
dataStore = load('relatedsig.mat');
% Assign variables for clarity
% T1, T2 are templates; S is the signal; Fs1, Fs2, Fs are sampling rates
template_A = dataStore.T1;
template_B = dataStore.T2;
measurement = dataStore.S;
rate_A = dataStore.Fs1;
rate_B = dataStore.Fs2;
rate_measurement = dataStore.Fs;
% Visualize the raw signals
figure;
ax(1) = subplot(3,1,1);
plot((0:numel(template_A)-1)/rate_A, template_A, 'k');
ylabel('Template A'); grid on;
title('Raw Signal Data');
ax(2) = subplot(3,1,2);
plot((0:numel(template_B)-1)/rate_B, template_B, 'r');
ylabel('Template B'); grid on;
ax(3) = subplot(3,1,3);
plot((0:numel(measurement)-1)/rate_measurement, measurement);
ylabel('Measurement'); grid on;
xlabel('Time (seconds)');
linkaxes(ax, 'x');
xlim([0 1.61]); ylim([-4 4]);
% Display sampling rates
disp('Sampling Rates (Template A, Template B, Measurement):');
disp([rate_A, rate_B, rate_measurement]);
To align the time bases, we calculate the rational approximation of the sampling rate ratio and apply the resample function to the templates.
% Calculate rational factors for resampling
[P1, Q1] = rat(rate_measurement / rate_A);
[P2, Q2] = rat(rate_measurement / rate_B);
% Upsample templates to match the measurement sampling rate
resampled_A = resample(template_A, P1, Q1);
resampled_B = resample(template_B, P2, Q2);
Detecting Patterns via Cross-Correlation
Once the sampling rates are synchronized, the xcorr function can be used to compute the cross-correlation between the measurement and the templates. This operation reveals the degree of similarity and identifies the presence of a specific patern within the measurement stream.
% Compute cross-correlation
[corr_A, lags_A] = xcorr(resampled_A, measurement);
[corr_B, lags_B] = xcorr(resampled_B, measurement);
% Plot correlation results
figure;
ax(1) = subplot(2,1,1);
plot(lags_A/rate_measurement, corr_A, 'k');
ylabel('Correlation Amplitude'); grid on;
title('Correlation: Measurement vs Template A');
ax(2) = subplot(2,1,2);
plot(lags_B/rate_measurement, corr_B, 'r');
ylabel('Correlation Amplitude'); grid on;
title('Correlation: Measurement vs Template B');
xlabel('Lag (seconds)');
axis(ax, [-1.5 1.5 -700 700]);
A distinct peak in the correlation plot indicates a match. The location of this peak corresponds to the time delay (lag) between the template and the signal's occurrence.
% Identify the location of the maximum correlation peak
[~, peakIndex] = max(abs(corr_B));
lagInSamples = lags_B(peakIndex);
lagInSeconds = lagInSamples / rate_measurement;
disp(['Time delay detected: ', num2str(lagInSeconds), ' seconds']);
disp(['Lead in samples: ', num2str(lagInSamples)]);
Delay Measurement and Signal Alignment
In multi-sensor environments, such as structural health monitoring where sensors are placed at different locations on a bridge, signals may arrive at different times due to the distance from the source. Aligning these signals is crucial for accurate analysis. By visualizing the data from three sensors (s1, s2, s3), we can observe the misalignment.
% Visualize multi-sensor data
figure;
ax(1) = subplot(3,1,1);
plot(s1); ylabel('Sensor 1'); grid on;
title('Multi-Sensor Vibration Data');
ax(2) = subplot(3,1,2);
plot(s2, 'k'); ylabel('Sensor 2'); grid on;
ax(3) = subplot(3,1,3);
plot(s3, 'r'); ylabel('Sensor 3'); grid on;
xlabel('Sample Index');
linkaxes(ax, 'xy');
Using the lag information derived from cross-correlation, these signals can be shifted to align the features of interest, allowing for coherent analysis of the event across all sensors.