# How can I merge the sine wave?

31 views (last 30 days)
taetae on 12 Jul 2024
Commented: Umar on 12 Jul 2024 at 17:55
there are space (200, 200, 20), and sensor that move (100, 1~200, 20).
I want to scan topographic features with sensor.
I know the distance from each sensor's location to the row coordinates of the sensor and the terrain, and the round-trip time via the speed of sound underwater (1500 m/s).
At this point, I want to scan the terrain by shooting a sine wave from each sensor's position toward a point.
I can get each sine wave, but I can't seem to get it to sum over rows, so I ask this question.
Below is the code I currently have written, but I think it's wrong
Fs = 10000; % sampling F
time_step = 1 / Fs;
max_time = max(L_values(:));%left side time value
t = 0:time_step:max_time;
combined_pulse_data = zeros(gridSize, length(t));
% generate sine wave and combine
for i = 1:100
for j = 1:gridSize
pulse_time = L_values(i, j);
if pulse_time > 0
pulse_index = round(pulse_time / time_step);
if pulse_index > 0 && pulse_index <= length(t)
combined_pulse_data(i, pulse_index:end) = combined_pulse_data(i, pulse_index:end) +
sin(2 * pi * 1000 * t(1:end-pulse_index+1));
end
end
end
end

Umar on 12 Jul 2024

Hi taetae,

Your goal is to shoot a sine wave from each sensor's position towards a point and then sum these waves over rows to scan the terrain. The current code you've written aims to generate sine waves and combine them, but you are facing challenges with summing them over rows effectively. Here is an approach for combining sine waves over rows based on your code structure:

Fs = 10000; % Sampling Frequency

time_step = 1 / Fs;

gridSize = 10; % Define the number of sensor positions

L_values = rand(100, gridSize); % Define L_values with random values

max_time = max(L_values(:)); % Maximum time value

t = 0:time_step:max_time;

combined_pulse_data = zeros(gridSize, length(t));

% Generate sine waves and combine

for i = 1:100 for j = 1:gridSize pulse_time = L_values(i, j); if pulse_time > 0 pulse_index = round(pulse_time / time_step);

`            if pulse_index > 0 && pulse_index <= length(t)`
`                % Generate sine wave`
`                sine_wave = sin(2 * pi * 1000 * t(1:end-pulse_index+1)); `
`                % Sum over rows efficiently`
```                combined_pulse_data(j, pulse_index:end) = combined_pulse_data(j, pulse_index:end) + sine_wave;
end
end
end
end```

% Display the results

figure;

imagesc(t, 1:gridSize, combined_pulse_data);

colorbar;

xlabel('Time');

ylabel('Sensor Position');

title('Combined Sine Waves for Topographic Scanning');

The above code snippet initializes parameters like sampling frequency, time steps, grid size, and random L_values. It then calculates the maximum time value and generates sine waves based on pulse times. The sine waves are summed efficiently over rows to create combined pulse data for each sensor position. Finally, the results are displayed as an image showing the combined sine waves over time and sensor positions. Feel free to customize this solution further based on your specific needs or additional functionalities required.

Please see attached code snippet generated in Matlab along with the plot.

Please let me know if you have any further questions.

taetae on 12 Jul 2024
I tried several things based on the code you gave me.
Your method is close to what I wanted as a result.
The result I want requires an additional condition, and even with the condition, it doesn't work well.
Below is the condition I added to your method, if the terrain is not flat and there is an object, the part that is not visible to the sensor has a L_value of 0. So the sine wave should not be captured in the part with a value of 0, but it is. Can you tell me what I am doing wrong?
for i = 1:100
for j = 1:gridSize
pulse_time = L_values(i, j);
if pulse_time > 0
pulse_index = round(pulse_time / time_step);
end
if pulse_index > 0 && pulse_index <= length(t)
% Generate sine wave
sine_wave = sin(2 * pi * 100 * t(1:end-pulse_index+1));
% Sum over rows efficiently
combined_pulse_data(j, pulse_index:end) = combined_pulse_data(j, pulse_index:end) + sine_wave;
end
end
end
Umar on 12 Jul 2024 at 17:55

Hi taetae,

You were missing the else block in your code. So, when L_value is 0, the else block should be triggered. Within this block, your combined_pulse_data for the current row j is set to zero, effectively excluding the capture of the sine wave data for that particular iteration.

`            if pulse_index > 0 && pulse_index <= length(t)`
`                % Generate sine wave`
`                sine_wave = sin(2 * pi * 100 * t(1:end-pulse_index+1)); `
`                % Sum over rows efficiently`
```                combined_pulse_data(j, pulse_index:end) = combined_pulse_data(j, pulse_index:end) + sine_wave;
end```
`        else`
`            % Handle the case where L_value is 0`
`            % Do not capture sine wave`
`            % Exclude sine wave capture by setting combined_pulse_data to zero`
`            combined_pulse_data(j, :) = 0;`

Feel free to customize this code snippet to suit your needs. Hope this will help resolve your problem now.

### Categories

Find more on Block Libraries in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!