7 views (last 30 days)

Show older comments

Muhammad Salem on 7 Nov 2023

Answered: Suraj Kumar about 3 hours ago

Open in MATLAB Online

please

I have two sources for sending signals, where x1 represents the signal from the first source and x2 represents the signal from the second source :

where :

w1: First Angular Frequncy (rad / sec) and frequncy 40 KHZ.

W2: Second Angular Frequncy (rad / sec) and frequncy 60 KHZ.

n: Number of samples

------------------------------------------------------------------------------------------------

I have this code, how can I add the following to it?

- How can I add the distance to x1 and x2 and find the distance between them ?
- How can I find location x1 and location x2 ?
- Plot Magnitude of FFT with frequencies ?

Please rewrite the code correctly to get the above results.

clear all

clc

close all

% Define all the parameters.

% Frequncy for wave one(KHZ).

f1 = 40e6 ;

% Frequncy for wave two(KHZ).

f2 = 60e6 ;

% The speed of light in vacuum (m/sec).

c = 3e8 ;

% Wavelength for wave one(m).

h1 = c / f1 ;

% Wavelength for wave two(m).

h2 = c / f2 ;

% Imaginary part.

j=sqrt(-1);

% Initialize the vector to store the values.

X = zeros(1, 20);

% Make for loop (where n: number of samples).

for n = 1:20;

x1=exp(j*h1*n);

x2=exp(j*h2*n);

X(n) = x1 + x2;

end

% Compute FFT.

Y = fft(X, 256);

% Compute Magnitude FFT

Y1 = abs(Y);

plot( Y1 );

grid on

title('Magnitude of FFT Spectrum of X(N)', 'FontSize',12,'Color','k');

##### 0 Comments Show -2 older commentsHide -2 older comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

### Answers (1)

Suraj Kumar 2 minutes ago

Open in MATLAB Online

Hi Muhammad,

To incorporate distance into signals, find their locations and plot the FFT magnitude, you can refer to the following steps and attached code snippets:

1. The phase shift due to distance was incorporated into the signals by multiplying by the term “exp (-j * 2 * pi * f * d / c)”. This term accounts for the phase delay introduced by the distance the signal travels.

x1 = exp(j * h1 * n) * exp(-j * 2 * pi * f1 * d1 / c);

x2 = exp(j * h2 * n) * exp(-j * 2 * pi * f2 * d2 / c);

X(n) = x1 + x2;

2. The signal locations were determined by distances (d1) and (d2) and printed to the console to indicate positions of (x1) and (x2).

% Display locations

disp(['Location of x1: ', num2str(d1), ' meters']);

disp(['Location of x2: ', num2str(d2), ' meters']);

3. The FFT of the combined signal was computed, and its magnitude was plotted against a normalized frequency axis, providing a clear visualization of the signal's frequency components.

% Compute FFT.

Y = fft(X, 256);

% Compute Magnitude FFT

Y1 = abs(Y);

% Frequency axis for plotting

Fs = 1;

f = (0:255) * (Fs / 256);

% Plot Magnitude of FFT with frequencies

figure;

plot(f, Y1);

grid on;

title('Magnitude of FFT Spectrum of X(N)', 'FontSize', 12, 'Color', 'k');

xlabel('Frequency (Hz)', 'FontSize', 12);

ylabel('Magnitude', 'FontSize', 12);

You may refer to the output below for a clearer understanding:

For more information on “fft” and ”abs” function, kindly go through the documentation below:

- https://www.mathworks.com/help/matlab/ref/fft.html
- https://www.mathworks.com/help/matlab/ref/abs.html

Hope it helps!

##### 0 Comments Show -2 older commentsHide -2 older comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

### See Also

### Categories

Signal ProcessingDSP System ToolboxTransforms and Spectral AnalysisTransforms

Find more on **Transforms** in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français

- United Kingdom(English)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)

Contact your local office