Problem with for loop
2 次查看(过去 30 天)
显示 更早的评论
My program can be broken into four parts:
1. Starting loop - asks the user what kind of filter (and window, if necessary) they want
2. work() - calculates 2 errors, and writes them onto a .txt file on disk.
3. createFilter() - creates the necessary filter, and return the vectors b and a
4. toWriteErrors() - creates a .txt file on disk to write the values of E1 and E2 to.
The main problem is in work(). The second for loop, the one that iterates 'w' from 0.1 to 0.9, is not producing values for the THIRD and SEVENTH iterations. Everywhere else it works fine. Just for those two iterations, it gives the values of E1 and E2 (the errors I am calculating) as NaN. I have no clue what is causing this. What do I do?
1. STARTING LOOP
while true
n = input('Enter the type of filter; 0 = IIR, 1 = FIR: ')
if n == 0
%IIR Create
expr = 'IIR';
expr
work(expr)
break
elseif n == 1
%FIR Create
type = input('Enter the windowing technique; \n0 = boxcar, 1 = hamming, 2 = hanning, 3 = kaiser: ')
if type == 0
%Boxcar create
expr = 'FIR Boxcar';
expr
work(expr)
break
elseif type == 1
%Hamming create
expr = 'FIR Hamming';
expr
work(expr)
break
elseif type == 2
%Hanning create
expr = 'FIR Hanning';
expr
work(expr)
break
else
%Kaiser create
expr = 'FIR Kaiser';
expr
work(expr)
break
end
else
disp 'Unrecognized command. Please enter either 0 or 1'
end
end
2. work()
%The Problematic function. Calculates values for E1 & E2, and writes them to disk
function work(expr)
fid = toWriteErrors(expr);
order = input('Enter max order of the filter: ')
Fsam = input('Enter sampling frequency in Hertz: ')
f = 0 : 0.01 : Fsam/2;
for k = 1:order
for w = 0.1:0.1:0.9 %The problematic loop. 3rd & 7th iterations not working
[b,a] = createFilter(expr,k,w);
H = freqz(b,a,f,Fsam);
fc = w * Fsam / 2;
p = find(f == fc);
PassBand = ones(1,p);
I1 = PassBand - H(1:p);
J1 = mean(I1);
E1 = 0;
E1 = abs(J1); %First error
fprintf(fid,'E1_%s_%s = %.4f\n',int2str(k),num2str(w,'%.1f'),E1);
StopBand = zeros(1,length(f)-p);
I2 = H(p+1:length(f)) - StopBand;
J2 = mean(I2);
E2 = 0;
E2 = abs(J2); %Second error
fprintf(fid,'E2_%s_%s = %.4f\n',int2str(k),num2str(w,'%.1f'),E2);
end
end
fclose(fid);
3. createFilter()
%Create filters
function [b,a] = createFilter(expr,i,w)
switch expr
case 'IIR'
[b,a] = butter(i,w);
case 'FIR Boxcar'
b = fir1(i,w,boxcar(i+1));
a = 1;
case 'FIR Hamming'
b = fir1(i,w);
a = 1;
case 'FIR Hanning'
b = fir1(i,w,hanning(i+1));
a = 1;
case 'FIR Kaiser'
b = fir1(i,w,kaiser(i+1));
a = 1;
end
4. toWriteErrors()
%Create filenames for .txt files for different filters
function fid = toWriteErrors(expr)
switch expr
case 'IIR'
filename = 'errors_iir.txt';
case 'FIR Boxcar'
filename = 'errors_boxcar.txt';
case 'FIR Hamming'
filename = 'errors_hamming.txt';
case 'FIR Hanning'
filename = 'errors_hanning.txt';
case 'FIR Kaiser'
filename = 'errors_kaiser.txt';
end
fid = fopen(filename,'w');
2 个评论
Jan
2013-3-19
What does "3rd & 7th iterations not working" exactly mean? As long as we only see the failing code, how could we suggest an improvement wiuthout using a crystal ball?
采纳的回答
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!