Advertisement
STANAANDREY

apd sapt 7 assig2

Nov 8th, 2023
1,310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.69 KB | None | 0 0
  1. function filter_signal(input_file, output_file)
  2.  
  3. % Constants
  4. F1 = 2000; % Frequency of the first component in Hz
  5. F2 = 3200; % Frequency of the second component in Hz
  6. Fs = 15000; % Sampling frequency in Hz
  7.  
  8. % Read the input audio file
  9. [x, Fs_original] = audioread(input_file);
  10.  
  11. % Generate the input signal x(t)
  12. t = 0:1/Fs:(length(x)-1)/Fs;
  13. input_signal = cos(2*pi*F1*t) + cos(2*pi*F2*t);
  14.  
  15. % Plot the input signal in the time domain
  16. figure;
  17. subplot(3, 1, 1);
  18. plot(t, input_signal);
  19. xlabel('Time (s)');
  20. ylabel('Amplitude');
  21. title('Input Signal in Time Domain');
  22.  
  23. % Compute and plot the magnitude of the signal's frequency response
  24. subplot(3, 1, 2);
  25. f = linspace(0, Fs/2, length(input_signal)/2);
  26. input_signal_fft = abs(fft(input_signal));
  27. plot(f, input_signal_fft(1:length(f)));
  28. xlabel('Frequency (Hz)');
  29. ylabel('Magnitude');
  30. title('Frequency Response of Input Signal');
  31.  
  32. % Design FIR filter to reject the first signal spectrum component
  33. cut_off_freq = F1 / (Fs / 2);
  34. filter_order = 50;
  35. b = fir1(filter_order, cut_off_freq, 'high');
  36.  
  37. % Apply the FIR filter on the input signal
  38. filtered_signal = filter(b, 1, input_signal);
  39.  
  40. % Plot the output signal in the time domain
  41. subplot(3, 1, 3);
  42. plot(t, filtered_signal);
  43. xlabel('Time (s)');
  44. ylabel('Amplitude');
  45. title('Output Signal in Time Domain');
  46.  
  47. % Compute and plot the magnitude of the filtered signal's frequency response
  48. figure;
  49. filtered_signal_fft = abs(fft(filtered_signal));
  50. plot(f, filtered_signal_fft(1:length(f)));
  51. xlabel('Frequency (Hz)');
  52. ylabel('Magnitude');
  53. title('Frequency Response of Filtered Signal');
  54.  
  55. % Write the filtered signal to an output audio file
  56. audiowrite(output_file, filtered_signal, Fs_original);
  57.  
  58. end
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement