Put Zero Value randomly in wave function

1 回表示 (過去 30 日間)
Rakan Akmal Musalim
Rakan Akmal Musalim 2022 年 3 月 19 日
回答済み: Atsushi Ueno 2022 年 3 月 19 日
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');

採用された回答

Star Strider
Star Strider 2022 年 3 月 19 日
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');
.

その他の回答 (1 件)

Atsushi Ueno
Atsushi Ueno 2022 年 3 月 19 日
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');

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by