我正在尝试重建这个算法:
http
://courses.csail.mit.edu/6.006/fall10/lectures/lec02.pdf
(第14页“算法II”)
(用谷歌找到这个,不幸的是我不在麻省理工学院: ) 这不是家庭作业)
这是:
•Pick middle column (j=m/2)
•Find global maximum a=A[i,m/2]in that column
(and quit if m=1)
•Compare a to b=A[i,m/2-1]and c=A[i,m/2+1]
•If b>a then recurse on left columns
•Else, if c>a then recurse on right columns
•Else a is a 2D peak!
我所拥有的是:
trimmed是一个向量,它保存我的大小频率(blockSizeSmall-minBlockSize)。
所以它是一个具有trimmed.size()
列和 (blockSizeSmall-minBlockSize) 行的二维矩阵。
为了简单起见,我将峰值保存在 2 个 int 向量vector<int>
peaksrow和peakscolumn中。
那里有什么问题?
我不明白什么
"Find global maximum a=A[i,m/2]in that column (and quit if m=1)"
应该导致。
public void findPeaks() {
for (int column = trimmed.size() / 2; column < trimmed.size();) {
int globalmax = 0;
for (int row = 0; row < (blockSizeSmall - minBlockSize); row++) {
if (trimmed.elementAt(column).reducedFreqs[row] > globalmax) {
globalmax = row;
//find globalmax in row
}
}
if (globalmax == 0) {
break; //<- ???
} else {
if (column - 1 >= 0 && column + 1 < trimmed.size()) {
//stay in bounds
if (trimmed.elementAt(column - 1).reducedFreqs[globalmax] > globalmax) {
column--;
//if element at left side is > globalmax, recurse on column--;
} else if (trimmed.elementAt(column + 1).reducedFreqs[globalmax] > globalmax) {
column++;
//if element at right side is > globalmax, recurse on column++;
} else {
//if globalmax is higher than right or left element i have a peak
peaksrown.add(globalmax);
peakscolumnn.add(column);
Log.e(TAG, "" + peaksrown.size());
}
}else{
//what to do when out of bounds ?? break ??
//if i break here, how can i be sure the algorithm
//went to both sides(column=0 and column=max) ??
//just tried with break here, still infinit loop
}
}
}
}
它只是无限循环。