Effacer les filtres
Effacer les filtres

Problem with wavelet code

5 vues (au cours des 30 derniers jours)
Mohammed Lamine Mekhalfia
Mohammed Lamine Mekhalfia le 20 Mar 2024
Dear MATLAB users
I have the following code which is used to select limited number of point from a vector and calclate the wavelet of the original vector and the new-sampled vector from the points we selected then calculate the wavelet energy ratio between the two vectors:
% Adjustable parameters
frequency = 58; % Frequency of the sine function
speed_rpm = 6000; % Rotation speed in rpm
duration = 60 / speed_rpm; % Duration of the signal for both high and low sampling rates
sampling_rate_high = 1668; % High sampling rate
num_samples_low = 16; % Number of samples for signal_low
% Time vector for signal_high
t_high = linspace(0, duration, duration * sampling_rate_high);
% Generate sine function for signal_high
signal_high = sin(2 * pi * frequency * t_high);
% Calculate the average of signal_high
average_signal_high = mean(signal_high);
% Select a subset of values from signal_high
step = floor(length(signal_high) / num_samples_low);
selected_indices = 1:step:length(signal_high);
selected_values = signal_high(selected_indices);
% Normalize selected subset to match the average of signal_high
average_selected_values = mean(selected_values);
normalized_selected_values = selected_values - (average_selected_values - average_signal_high);
% Linear interpolation to match the length of t_low
t_low = linspace(0, duration, num_samples_low);
normalized_selected_values_interp = interp1(linspace(0, duration, length(normalized_selected_values)), normalized_selected_values, t_low, 'linear', 'extrap');
% Construct signal_low
signal_low = normalized_selected_values_interp;
% Compute wavelet transform for both high and low sampling rates
scales = 1:64; % Choose appropriate scales for wavelet analysis
coefficients_high = cwt(signal_high, scales, 'db4');
coefficients_low = cwt(signal_low, scales, 'db4');
% Calculate the wavelet energy ratio
energy_ratio = zeros(1, length(scales));
for i = 1:length(scales)
energy_ratio(i) = sum(abs(coefficients_low(i, :)).^2) / sum(abs(coefficients_high(i, :)).^2);
end
% Plotting
figure;
% Original signal (signal_high)
subplot(3, 2, 1);
plot(t_high, signal_high, 'b');
title('Original Signal (High Sampling Rate)');
xlabel('Time');
ylabel('Amplitude');
% Signal_low
subplot(3, 2, 2);
stem(selected_indices, selected_values, 'r', 'Marker', 'o');
hold on;
plot(t_low, signal_low, 'b');
title('Signal Low Sampling Rate');
xlabel('Index');
ylabel('Amplitude');
legend('Selected Values', 'Interpolated Signal');
% Wavelet transform for signal_high
subplot(3, 2, 3);
imagesc(t_high, scales, abs(coefficients_high));
colorbar;
title('Wavelet Transform (High Sampling Rate)');
xlabel('Time');
ylabel('Scale');
% Wavelet transform for signal_low
subplot(3, 2, 4);
imagesc(t_low, scales, abs(coefficients_low));
colorbar;
title('Wavelet Transform (Low Sampling Rate)');
xlabel('Time');
ylabel('Scale');
% Energy ratio as a function of scale
subplot(3, 2, [5 6]);
plot(scales, energy_ratio, 'LineWidth', 1.5);
title('Wavelet Energy Ratio');
xlabel('Scale');
ylabel('Energy Ratio');
% Display the average energy ratio
disp(['Average Wavelet Energy Ratio: ', num2str(mean(energy_ratio))]);
I am facing a problem, I am playing with the adjustable paramters but when I put the sampling rate lower than 1668 the code stop to work:
also when I change the speed this affect the number of samples, I know I missing something but I have no idea about it.
speed_rpm = 6000; % Rotation speed in rpm
duration = 60 / speed_rpm; % Duration of the signal for both high and low sampling rates
sampling_rate_high = 1668; % High sampling rate
num_samples_low = 16; % Number of samples for signal_low
the second part of my question:
I would like to implement a code that reduce the number of samples taken into consideration and define the appropriate positions (unifrom or non uniform arraangement) without losing the information of the original signal. for this I am using the wavelet energy ratio as an indicator but to compute all the possible arrangement it would be computationally cost so I am thinking about integrating Genetic Algorithms GA. to make it but I have no idea about it, I know just the theory of GA so if someone can set me on the path I will be very greatful for it.
Thanks a lot for reading and answering :) .

Réponses (0)

Catégories

En savoir plus sur Continuous Wavelet Transforms dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by