1 dimensional convolution in matlab

To perform a 1 dimensional convolution in Matlab, you can use the built-in function conv. Here is an example of how to use it:

main.m
% create a signal to filter
x = linspace(0, 2*pi, 100);
y = sin(x);

% create a filter kernel
kernel = ones(1, 5) / 5; % averaging filter

% perform convolution
y_filtered = conv(y, kernel, 'same');

% plot results
figure;
plot(x, y, 'b-', x, y_filtered, 'r-');
legend('Original Signal', 'Filtered Signal');
xlabel('Time (s)');
ylabel('Amplitude');
349 chars
17 lines

In this example, we first create a sinusoidal signal to filter (y). We then create a filter kernel that applies a moving average over 5 samples. We then use the conv function to filter the signal with the kernel. The 'same' option specifies that the output is the same size as the input. We then plot the original signal (y) in blue and the filtered signal (y_filtered) in red.

gistlibby LogSnag