MATLAB Frequency Response  - Page 1
Forum section: electronics / beginners

author: gana24
author_rank: Newbie
timestamp: May 31, 2025, 02:37:52 am
content: Good day, everyone. I have been working on MATLAB for my Digital Signal Processing course. One of the task is to get Frequency Response for an ECG signal (given) and the IIR/FIR filters. When I used the fft() function, I was not able to get a clean-looking frequency response. Well, I just used the code in the note, it works for the example, and I get nice looking graphs but when I use the ECG signal sample given, all I get is a messy looking graph. I am not sure what went wrong so I am seeking the help of the seniors here.MATLAB Code:load ecg.matx = val(1,:);X = fft(x);Xmag = abs(X);Xphase = angle(X);X = fft(x);Xmag = abs(X);Xphase = angle(X);%Mapping discrete frequency k to wN = length(x);k = 0:N-1;w = 2*pi*k/N;%Plotting Magnitude Responsefiguresubplot(2,1,1), plot(w/pi, Xmag)xlabel('\omega (rad)'), ylabel('|X(\omega)|')xticks(0:0.2:2), xtickformat('%g\x3c0')title('DTFT Magnitude Response')grid on%Plotting Phase Responsesubplot(2,1,2), plot(w/pi, Xphase/pi)xlabel('\omega (rad)'), ylabel('\angle X(\omega)')xticks(0:0.2:2), xtickformat('%g\x3c0')yticks(-1:1), ytickformat('%g\x3c0')title('DTFT Phase Response')grid on

author: radiolistener
author_rank: Super Contributor
timestamp: May 31, 2025, 10:26:24 am
content: if you want to plot frequency response of FIR or IIR filter you can simply use matlab/octave function freqz, for example:Code:pkg load signal; % for octave% generate FIR filter coefsFs = 2000; % Sampling frequencyFc = 500; % Cutoff frequencyN = 512; % Filter orderb = fir1(N, Fc/(Fs/2)); % FIR LPF design, normalized cutoffa = 1; % FIR: denominator is just 1% plot FIR filter frequency and phase responsefreqz(b, a, 2^16, Fs); % Plot frequency response with fft size 2^16grid on;wherebanda- are filter coefficients vectors.bis numerator coefficientsais denominator coefficientsFor FIR a=1 and b contains FIR kernel.For IIR both will contains vectors with coefficients.if you want to plot signal spectrum:Code:pkg load signal; % load package (required for octave)Fs = 48000;N = 2^16;% generate two-tone signal vectorsignal = cos(700 * 2*pi*(0:N-1)/Fs) + ... % 700 Hz sinecos(1200 * 2*pi*(0:N-1)/Fs); % 1200 Hz sine% generate Hann window of size Nwindow = hann(N)';% apply window and get fft with normalization for size Nfft_signal = fft(signal .* window / sum(window), N);% prepare data for plotf = (0:N/2) * (Fs/N); % Frequency vector for plottingmagnitude = abs(fft_signal(1:(N/2+1))); % Take the magnitude of the first half of the FFTmagnitude_dB = 20 * log10(magnitude + eps); % Convert to dB scale relative to full-scale amplitude (+eps to avoid -Inf for 0)% find min/max range to displaymin_dB = min(magnitude_dB)-10;max_dB = max(magnitude_dB)+10;freq_min = 0;freq_max = Fs/2;% plot fftfigure;plot(f, magnitude_dB);xlabel('Hz');ylabel('dBFS');title(sprintf('FFT %d pts, Fs=%d Hz', N, Fs));xlim([freq_min freq_max]);ylim([min_dB max_dB]);grid on;grid minor;

author: radiolistener
author_rank: Super Contributor
timestamp: May 31, 2025, 10:51:40 am
content: Combining both (generate two-tone signal with 700+1200 Hz and apply FIR filter with cut-off 1000 Hz, then show spectrum of both signals):Code:pkg load signal; % for octaveFs = 48000; % Sampling frequencyFc = 1000; % Cutoff frequencyN = 512; % Filter orderb = fir1(N, Fc/(Fs/2)); % FIR LPF design, normalized cutoffa = 1; % FIR: denominator is just 1freqz(b, a, 2^16, Fs); % Plot frequency response with fft size 2^16grid on;N = 2^16; % signal length% generate two-tone signal vectorsignal = cos(700 * 2*pi*(0:N-1)/Fs) + ... % 700 Hz sinecos(1200 * 2*pi*(0:N-1)/Fs); % 1200 Hz sine% apply FIR filter to the signalsignal_filtered = filter(b, a, signal);% generate Hann windowwindow = hann(N)'; % Hann window of size N% apply window and get fft with normalization for size Nfft_signal = fft(signal .* window / sum(window), N);fft_signal_filtered = fft(signal_filtered .* window / sum(window), N);% prepare data for plotf = (0:N/2) * (Fs/N); % Frequency vector for plottingmagnitude = abs(fft_signal(1:(N/2+1))); % Take the magnitude of the first half of the FFTmagnitude_dB = 20 * log10(magnitude + eps); % Convert to dB scale relative to full-scale amplitude% the same for filtered signalmagnitude_filtered = abs(fft_signal_filtered(1:(N/2+1)));magnitude_filtered_dB = 20 * log10(magnitude_filtered + eps);% find min/max range to displaymin_dB = min(magnitude_dB)-10;max_dB = max(magnitude_dB)+10;freq_min = 0;freq_max = 2000; % show 0..2000 Hz range% plot fftfigure;hold on;plot(f, magnitude_dB, 'b-'); % blue color for original signalplot(f, magnitude_filtered_dB, 'r-'); % red color for filtered signalxlabel('Hz');ylabel('dBFS');title(sprintf('FFT %d pts, Fs=%d Hz', N, Fs));xlim([freq_min freq_max]);ylim([min_dB max_dB]);grid on;grid minor;I'm not familiar with proprietary mat file format, because I'm using Octave, so I cannot help to properly load it.But if you want to load let's say standard audio file like WAV, MP3, etc, you can load it in the following way:Code:[signal, Fs] = audioread('test.wav');or if you want to load only specific range of samples, lets say first 100000 samples:Code:[signal, Fs] = audioread('test.wav', [1 100000]);You can also save filtered signal to audio file:Code:audiowrite('test-filtered.wav', signal_filtered, Fs, 'BitsPerSample', 32);In Octave, using audioread/audiowrite needs to load audio package:Code:pkg load audio;In MATLAB package load is not required, so you can remove these pkg load lines.