Borrar filtros
Borrar filtros

Problem with for loop

3 visualizaciones (últimos 30 días)
Srikanth
Srikanth el 19 de Mzo. de 2013
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 comentarios
Jan
Jan el 19 de Mzo. de 2013
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?
Srikanth
Srikanth el 19 de Mzo. de 2013
Oops. I thought I'd explained it properly. Okay here goes. I'm trying to calculate E1 and E2, basically. In the for loop that I marked with that comment, the values for all iterations except the 3rd and 7th are working just fine. That is, they're producing values (or numbers) for E1 and E2. But the 3rd (w = 0.3) and 7th (w = 0.7) were outputting "E1 = NaN, and E2 = NaN". And I couldn't figure out why those two iterations were so cruel to me. It turns out its because the values 0.3 and 0.7 are actually approximated, not precise. Well, thanks to a Mr. Walter Roberson, its all good now.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 19 de Mzo. de 2013
  1 comentario
Srikanth
Srikanth el 19 de Mzo. de 2013
Worked like a charm! Thank you so much.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matched Filter and Ambiguity Function en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by