Put Zero Value randomly in wave function

1 view (last 30 days)
So i have a sin wave function and i want to put zero value number randomly into the wave function. Is that possible and how to ?
%Sine wave
freq=400;
Amp=2;
t_samp=1/8000;
T=0.01;
t=0:t_samp:T;
y=Amp*sin(2*pi*freq*t);
subplot(2,1,1);
plot(t,y)
ylabel ('Amplitude');
xlabel ('Time');
title ('Sine wave');

Accepted Answer

Star Strider
Star Strider on 19 Mar 2022
Try something like this —
%Sine wave
freq=400;
Amp=2;
t_samp=1/8000;
T=0.01;
t=0:t_samp:T;
y=Amp*sin(2*pi*freq*t);
y(randi(numel(y))) = 0; % Random Value (Index) = 0
subplot(2,1,1);
plot(t,y)
ylabel ('Amplitude');
xlabel ('Time');
title ('Sine wave');
.

More Answers (1)

Atsushi Ueno
Atsushi Ueno on 19 Mar 2022
Yes it is possible. You can put zero value by making random value index of y.
%Sine wave
freq=400;
Amp=2;
t_samp=1/8000;
T=0.01;
t=0:t_samp:T;
y=Amp*sin(2*pi*freq*t);
y(randi([1 numel(y)],1,10)) = 0; % ----- here
subplot(2,1,1);
plot(t,y)
ylabel ('Amplitude');
xlabel ('Time');
title ('Sine wave');

Categories

Find more on Mathematics 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!