How can I add White Gaussian Noise?

31 Ansichten (letzte 30 Tage)
Zaref Li
Zaref Li am 5 Jan. 2024
Kommentiert: Zaref Li am 5 Jan. 2024
Hi everyone,
I want to do this :
I wrote this code but I get errors. What can I do?
% Signal parameters
Fs = 250; % Sampling frequency
t = 0:1/Fs:49/Fs; % Time vector (50 samples at 250 Hz sampling rate)
frequencies = [60, 70, 100]; % Frequencies of sinusoids
amplitudes = [1, 1, 1]; % Amplitudes of sinusoids
% Generate signal composed of sinusoids
signal = sum(amplitudes' * sin(2*pi*frequencies'*t), 1); % Generate signal
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.
% Additive white Gaussian noise
noise_power = 10^(-6/10); % Power in dB
noise = sqrt(noise_power) * randn(1, length(t)); % Generate noise
% Signal with noise
signal_with_noise = signal + noise;
% Plot the generated signal with noise
figure;
subplot(2,1,1);
plot(t, signal);
title('Original Signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2,1,2);
plot(t, signal_with_noise);
title('Signal with Additive White Gaussian Noise');
xlabel('Time (s)');
ylabel('Amplitude');
%ERROR
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the
number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
Related documentation

Akzeptierte Antwort

Hassaan
Hassaan am 5 Jan. 2024
Bearbeitet: Hassaan am 5 Jan. 2024
  1. Generate three sinusoids with frequencies of 60 Hz, 70 Hz, and 100 Hz.
  2. Ensure that each sinusoid has a power of 1 (unity power).
  3. Add white Gaussian noise with a power of -6 dB relative to the signal power.
  4. Sample the signal at a rate of 250 Hz.
To calculate the noise power, you need to relate it to the signal power. If the signal has a power of 1 (0 dB), then the noise needs to have a power of 0.5 (since -6 dB is half the power on a linear scale).
fs = 250; % Sampling frequency in Hz
t = (0:49) / fs; % Time vector for 50 samples
frequencies = [60, 70, 100]; % Frequencies of the sinusoids
% Generate the sinusoids
signal = cos(2 * pi * frequencies(1) * t) + ...
cos(2 * pi * frequencies(2) * t) + ...
cos(2 * pi * frequencies(3) * t);
% Normalize the signal to have unity power
signal = signal / sqrt(mean(signal.^2));
% Generate the noise at -6 dB relative to the signal power i.e -6 DB = 1/4 = 0.25
% in linear scale
noise_power_db = -6; % Noise power in dB
noise_power_linear = 10^(noise_power_db / 10); % Convert dB to linear scale
noise = sqrt(noise_power_linear / 2) * randn(1, length(t)); % Generate noise
% Add the noise to the signal
noisy_signal = signal + noise;
% Plot the signal and the noisy signal
figure;
subplot(2, 1, 1);
plot(t, signal);
title('Original Signal');
grid on
subplot(2, 1, 2);
plot(t, noisy_signal);
title('Noisy Signal');
grid on
This code generates the required signal and noise, adds them together, and then plots both the original clean signal and the noisy signal. Please adjust the code if needed to fit the specific requirements of your application or simulation environment.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
  3 Kommentare
Hassaan
Hassaan am 5 Jan. 2024
Bearbeitet: Hassaan am 5 Jan. 2024
@ScottB Thank you for pointing out the mistake. I have updated my solution.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Zaref Li
Zaref Li am 5 Jan. 2024
thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 5 Jan. 2024
frequencies is 1 x 3. frequencies' is 3 x 1.
t is 1 x 50 (claimed in the code)
2*pi*frequencies'*t would be 3 x 1 * 1 x 50, giving a 3 x 50 result.
amplitudes is 1 x 3. amplitudes' is 3 x 1
amplitudes' * sin(2*pi*frequencies'*t)
would be 3 x 1 * 3 x 50 . That is an error for the * operator.
amplitudes' .* sin(2*pi*frequencies'*t)
would probably work.

Community Treasure Hunt

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

Start Hunting!

Translated by