Untitled

 avatar
unknown
plain_text
a year ago
1.8 kB
6
Indexable
function create_filters()
    fs = input('Enter the sampling frequency fs (Hz): ');
    N = input('Enter the number of samples N: ');
    filter_type = input('Enter filter type (LPF, HPF, BPF, BSF): ', 's');

    switch filter_type
        case 'LPF'
            cutoff_freq = input('Enter cutoff frequency (Hz): ');
        case 'HPF'
            cutoff_freq = input('Enter cutoff frequency (Hz): ');
        case 'BPF'
            cutoff_freq1 = input('Enter lower cutoff frequency (Hz): ');
            cutoff_freq2 = input('Enter upper cutoff frequency (Hz): ');
        case 'BSF'
            cutoff_freq1 = input('Enter lower cutoff frequency (Hz): ');
            cutoff_freq2 = input('Enter upper cutoff frequency (Hz): ');
        otherwise
            error('Invalid filter type entered.');
    end


    DF = (0:N-1) / N;
    DF = DF - 0.5;


    CF = DF * fs;


    H = zeros(1, N);
    switch filter_type
        case 'LPF'
            H(abs(DF) < cutoff_freq/fs) = 1;
        case 'HPF'
            H(abs(DF) > cutoff_freq/fs) = 1;
        case 'BPF'
            H((abs(DF) > cutoff_freq1/fs) & (abs(DF) < cutoff_freq2/fs)) = 1;
        case 'BSF'
            H((abs(DF) < cutoff_freq1/fs) | (abs(DF) > cutoff_freq2/fs)) = 1;
    end


    figure;
    subplot(2,1,1);
    stem(DF, H);
    title(['Filter Profile in DF Domain (' filter_type ')']);
    xlabel('Normalized Discrete Frequency');
    ylabel('Magnitude');
    xlim([-0.5 0.5]);

    subplot(2,1,2);
    plot(CF, H, 'LineWidth', 2);
    title(['Filter Profile in CF Domain (' filter_type ')']);
    xlabel('Continuous Frequency (Hz)');
    ylabel('Magnitude');
    xlim([-fs/2 fs/2]);

    saveas(gcf, [filter_type '_Filter_N' num2str(N) '_fs' num2str(fs) '.png']);
end
Editor is loading...
Leave a Comment