- ‘cwt’ function: https://www.mathworks.com/help/wavelet/ref/cwt.html
- ‘optimoptions’ function: https://www.mathworks.com/help/optim/ug/optim.problemdef.optimizationproblem.optimoptions.html
- ‘ga’ function: https://www.mathworks.com/help/gads/ga.html
- ‘interp1’ function: https://www.mathworks.com/help/matlab/ref/interp1.html

# Problem with wavelet code

2 views (last 30 days)

Show older comments

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 :) .

##### 0 Comments

### Answers (1)

Suraj Kumar
on 28 Aug 2024

Hi Mohammed,

From what I gather you want to ensure the working of signal processing correctly across different sampling speeds and aim to utilize Genetic Algorithms (GA) to optimize the selection of sample indices.

To accomplish this, you can consider following these steps and refer to the attached code snippets:

1. Create a signal based on given frequency and sampling rate and compute its continuous wavelet transform (CWT) to analyse its time-scale characteristics.

% Generate sine function for signal_high

signal_high = sin(2 * pi * frequency * t_high);

average_signal_high = mean(signal_high);

% Compute wavelet transform for the high sampling rate

scales = 1:64;

coefficients_high = cwt(signal_high, scales, 'db4');

2. Configure a Genetic Algorithm (GA) to find the optimal indices for down sampling. The GA aims to maximize the preservation of wavelet energy by selecting the best indices from ‘signal_high’.

% Define the fitness function

function energy_ratio = wavelet_energy_fitness(selected_indices, signal_high, scales, average_signal_high, duration, num_samples_low)

selected_indices = round(selected_indices);

selected_values = signal_high(selected_indices);

average_selected_values = mean(selected_values);

normalized_selected_values = selected_values - (average_selected_values - average_signal_high);

% Interpolate 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');

% Compute wavelet transform for low sampling rate

coefficients_low = cwt(normalized_selected_values_interp, scales, 'db4');

coefficients_high = cwt(signal_high, scales, 'db4');

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

energy_ratio = -mean(energy_ratio);

end

3. Use the GA to determine the optimal indices ‘selected_indices_opt’. Select and normalize these indices from ‘signal_high’ to create a down sampled signal ‘signal_low_opt’.

% Use the GA to find optimal indices

num_samples_high = length(signal_high);

[selected_indices_opt, fval] = ga(@(indices) wavelet_energy_fitness(indices, signal_high, scales, average_signal_high, duration, num_samples_low), ...

num_samples_low, [], [], [], [], ...

ones(1, num_samples_low), num_samples_high * ones(1, num_samples_low), ...

[], options);

% Construct signal_low using optimal indices

selected_values_opt = signal_high(round(selected_indices_opt));

average_selected_values_opt = mean(selected_values_opt);

normalized_selected_values_opt = selected_values_opt - (average_selected_values_opt - average_signal_high);

For better understanding, you can refer to the output below:

For more insights, kindly refer to the documentation links below:

Hope this helps!

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!