How to write a code in Matlab having two conditions?
    조회 수: 4 (최근 30일)
  
       이전 댓글 표시
    
    Samson David Puthenpeedika
      
 2021년 11월 19일
  
    
    
    
    
    댓글: Samson David Puthenpeedika
      
 2021년 11월 20일
            how to create rectangle filter  with following condition;
f(t)= 1;   -0.1secs< t < 0.1 secs
f(t)= 0;  otherwise
Below i have tried but getting error.
n=2048;  %samples
fs=1024; %sampling frequency
T=1/fs;
t = (0:n-1)*T;
F=zeros(length(t));
for i=0:length(t)
    if t(i)<=0.1
        F(i)=1;
    else
        F(i)=0;
    end
end
채택된 답변
  Dave B
    
 2021년 11월 20일
        
      편집: Dave B
    
 2021년 11월 20일
  
      In MATLAB indices start with 1:
n=2048;  %samples
fs=1024; %sampling frequency
T=1/fs;
t = (0:n-1)*T;
F=zeros(length(t));
for i=1:length(t)
    if abs(t(i))<=0.1  % technically better because it covers the negative case
        F(i)=1;
    else
        F(i)=0; % not really needed because you initialized F with all zeros...
    end
end
But note, you don't need a loop for this kind of thing:
F2=zeros(length(t));
F2(abs(t)<=.1)=1;
isequal(F2,F)
댓글 수: 0
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!