本文共 5508 字,大约阅读时间需要 18 分钟。
注意:卷积运算的结果与滤波器的大小有关,输出信号的维度会根据滤波器的大小而变化。
```objective-c - (NSArray *)oneDimensionalConvolution:(NSArray *)inputArray withFilter:(NSArray *)filterArray { // 计算输出数组的长度 NSInteger outputLength = [inputArray length] + [filterArray length] - 1; NSMutableArray *outputArray = [[NSMutableArray alloc] init]; // 遍历输入数组,进行卷积计算 for (NSInteger i = 0; i < [inputArray length]; i++) { NSInteger filterStartIndex = max(0, i - [filterArray length] + 1); NSInteger filterEndIndex = min([inputArray length] - 1, i + [filterArray length] - 1); NSInteger sum = 0; for (NSInteger j = filterStartIndex; j <= filterEndIndex; j++) { sum += [inputArray[j] * filterArray[j - filterStartIndex]]; } [outputArray addObject:sum]; } return [outputArray autorelease]; } ``` 说明:上述代码实现了一个简单的一维卷积算法,通过遍历输入数组并与滤波器进行点积,生成输出数组。 ```objective-c - (NSArray *)twoDimensionalConvolution:(NSArray *)inputMatrix withFilter:(NSArray *)filterMatrix { // 计算输出矩阵的尺寸 NSInteger inputRows = [inputMatrix length]; NSInteger inputColumns = [inputMatrix[0] length]; NSInteger filterRows = [filterMatrix length]; NSInteger filterColumns = [filterMatrix[0] length]; NSInteger outputRows = inputRows + filterRows - 1; NSInteger outputColumns = inputColumns + filterColumns - 1; NSMutableArray *outputMatrix = [[NSMutableArray alloc] init]; // 遍历输入矩阵,进行卷积计算 for (NSInteger i = 0; i < inputRows; i++) { for (NSInteger j = 0; j < inputColumns; j++) { NSInteger filterRowStart = max(0, i - filterRows + 1); NSInteger filterRowEnd = min(inputRows - 1, i + filterRows - 1); NSInteger filterColumnStart = max(0, j - filterColumns + 1); NSInteger filterColumnEnd = min(inputColumns - 1, j + filterColumns - 1); NSInteger sum = 0; for (NSInteger fi = filterRowStart; fi <= filterRowEnd; fi++) { for (NSInteger fj = filterColumnStart; fj <= filterColumnEnd; fj++) { sum += [inputMatrix[fi][fj] * filterMatrix[fi - filterRowStart][fj - filterColumnStart]]; } } [outputMatrix addObject:sum]; } } return [outputMatrix autorelease]; } ``` 说明:上述代码实现了一个简单的二维卷积算法,通过遍历输入矩阵并与滤波器进行二维点积,生成输出矩阵。 ```objective-c #import@interface Convolution : NSObject - (NSArray *)oneDimensionalConvolution:(NSArray *)inputArray withFilter:(NSArray *)filterArray; - (NSArray *)twoDimensionalConvolution:(NSArray *)inputMatrix withFilter:(NSArray *)filterMatrix; - (NSArray *)convolve:(NSArray *)array withFilter:(NSArray *)filter; - (NSArray *)convolve2D:(NSArray *)matrix withFilter:(NSArray *)filter; - (void)printResult:(id)result; - (void)printMatrix:(NSArray *)matrix;
@end
@implementation Convolution - (NSArray *)oneDimensionalConvolution:(NSArray *)inputArray withFilter:(NSArray *)filterArray { NSInteger inputLength = [inputArray length]; NSInteger filterLength = [filterArray length]; if (inputLength == 0 || filterLength == 0) { return [NSArray array]; } NSInteger outputLength = inputLength + filterLength - 1; NSMutableArray *outputArray = [[NSMutableArray alloc] init]; for (NSInteger i = 0; i < inputLength; i++) { NSInteger filterStart = max(0, i - filterLength + 1); NSInteger filterEnd = min(inputLength - 1, i + filterLength - 1); NSInteger sum = 0; for (NSInteger j = filterStart; j <= filterEnd; j++) { sum += [inputArray[j] * filterArray[j - filterStart]]; } [outputArray addObject:sum]; } return [outputArray autorelease]; } - (NSArray *)twoDimensionalConvolution:(NSArray *)inputMatrix withFilter:(NSArray *)filterMatrix { NSInteger rows = [inputMatrix length]; if (rows == 0) return [NSArray array]; NSInteger cols = [inputMatrix[0] length]; NSInteger filterRows = [filterMatrix length]; if (filterRows == 0) return [NSArray array]; NSInteger filterCols = [filterMatrix[0] length]; NSInteger outputRows = rows + filterRows - 1; NSInteger outputCols = cols + filterCols - 1; NSMutableArray *outputMatrix = [[NSMutableArray alloc] init]; for (NSInteger i = 0; i < rows; i++) { for (NSInteger j = 0; j < cols; j++) { NSInteger rowStart = max(0, i - filterRows + 1); NSInteger rowEnd = min(rows - 1, i + filterRows - 1); NSInteger colStart = max(0, j - filterCols + 1); NSInteger colEnd = min(cols - 1, j + filterCols - 1); NSInteger sum = 0; for (NSInteger fi = rowStart; fi <= rowEnd; fi++) { for (NSInteger fj = colStart; fj <= colEnd; fj++) { sum += [inputMatrix[fi][fj] * [filterMatrix[fi - rowStart][fj - colStart]]]; } } [outputMatrix addObject:sum]; } } return [outputMatrix autorelease]; } - (void)printResult:(id)result { NSLog(@"结果:%@", result); } - (void)printMatrix:(NSArray *)matrix { for (NSInteger i = 0; i < [matrix length]; i++) { NSLog(@"行 %d:%@", i, matrix[i]); } } @end 转载地址:http://yiifk.baihongyu.com/