How can I add White Gaussian Noise?
Show older comments
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
% 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
Accepted Answer
More Answers (1)
Walter Roberson
on 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.
Categories
Find more on Spectral Measurements 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!